#include<cstdio>
#include<algorithm>
using namespace std;

struct vi
{
	int start;
	int end;
	int cost;
}edge[5000],et;

int p[100];

int Find(int n)
{
	return n==p[n]?n:p[n]=Find(p[n]);
}

bool cmp(vi a,vi b)
{
	return a.cost<b.cost;
}

int main()
{
	int n,i,k,sum,ST,EN,a,b,c,d;
	while(scanf("%d",&n)!=EOF,n)
	{
		for(i=1;i<=n;i++)
			p[i]=i;
		for(k=i=0;i<n*(n-1)/2;i++)
		{
			scanf("%d%d%d%d",&a,&b,&c,&d);
			if(d)
				p[Find(a)]=Find(b);
			else
			{
				et.start=a;
				et.end=b;
				et.cost=c;
				edge[k++]=et;
			}
		}
		sort(edge,edge+k,cmp);
		for(sum=i=0;i<k;i++)
		{
			ST=Find(edge[i].start);
			EN=Find(edge[i].end);
			if(ST!=EN)
			{
				p[ST]=EN;
				sum+=edge[i].cost;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}
/**************************************************************
	Problem: 2205
	User: admin
	Language: C++
	Result: Accepted
	Time:11 ms
	Memory:1208 kb
****************************************************************/