import java.nio.file.*;
import java.util.*;

public class Euler107 {
    static int[] parent, rank_;

    static int find(int x) {
        while (parent[x] != x) {
            parent[x] = parent[parent[x]];
            x = parent[x];
        }
        return x;
    }

    static boolean union(int x, int y) {
        int rx = find(x), ry = find(y);
        if (rx == ry)
            return false;
        if (rank_[rx] < rank_[ry]) {
            int t = rx;
            rx = ry;
            ry = t;
        }
        parent[ry] = rx;
        if (rank_[rx] == rank_[ry])
            rank_[rx]++;
        return true;
    }

    public static void main(String[] args) throws Exception {
        List<String> lines = Files.readAllLines(Path.of("resources/documents/0107_network.txt"));
        int n = lines.size();
        List<int[]> edges = new ArrayList<>();
        int totalW = 0;
        for (int i = 0; i < n; i++) {
            String[] p = lines.get(i).split(",");
            for (int j = i + 1; j < n; j++)
                if (!p[j].equals("-")) {
                    int w = Integer.parseInt(p[j]);
                    edges.add(new int[] { w, i, j });
                    totalW += w;
                }
        }
        edges.sort((a, b) -> a[0] - b[0]);
        parent = new int[n];
        rank_ = new int[n];
        for (int i = 0; i < n; i++)
            parent[i] = i;
        int mstW = 0;
        for (int[] e : edges)
            if (union(e[1], e[2]))
                mstW += e[0];
        System.out.println(totalW - mstW);
    }
}
