import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {
private static int[] parents;
private static class Edge {
private final int from, to, weight;
private Edge(int from, int to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
if (n == 0) break;
List<Edge> edges = new ArrayList<>();
for (int i = 0; i < n * (n - 1) / 2; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
int weight = sc.nextInt();
int flag = sc.nextInt();
if (flag == 1) weight = 0;
edges.add(new Edge(from, to, weight));
}
edges.sort(Comparator.comparingInt(o -> o.weight));
parents = new int[n + 1];
for (int i = 1; i <= n; i++) parents[i] = i;
int ans = 0;
for (Edge edge : edges) {
int a = find(edge.from);
int b = find(edge.to);
if (a != b) {
parents[a] = b;
ans += edge.weight;
}
}
System.out.println(ans);
}
}
private static int find(int x) {
return x == parents[x] ? x : (parents[x] = find(parents[x]));
}
}