fun main(args: Array<String>) {
    val inf = 10000
    val n = readLine()!!.toInt();
    val a = Array(n) { readLine()!!.split(" ").drop(1).map { it.toInt() }.map { if (it < 0) inf else it } }
    val c = Array(n) { 0 }
    val d = Array(n) { inf }
    val p = Array(n) { -1 }
    d[0] = 0
    while (true) {
        var mincost = inf
        var u = -1
        for (i in 0 until n) {
            if (c[i] != 2 && d[i] < mincost) {
                mincost = d[i]
                u = i
            }
        }
        if (u == -1)
            break
        c[u] = 2
        for (j in 0 until n) {
            if (c[j] != 2 && a[u][j] != inf) {
                if (d[j] > a[u][j]) {
                    d[j] = a[u][j]
                    p[j] = u
                    c[j] = 1
                }
            }
        }
    }
    var sum = 0
    for (i in 0 until n) if (p[i] != -1) sum += a[i][p[i]]
    println(sum)
}


