object Main extends App {
    def find(m: Int, ar: Array[Int]): Int = 
        if(ar(m) == m) m else {ar(m) = find(ar(m), ar); ar(m)}
 
    def unite(x: Int, y: Int, ar: Array[Int]) = {
        val _x = find(x, ar)
        val _y = find(y, ar)
        if(_x != _y) ar(_x) = _y
    }
 
    val in = io.Source.stdin.getLines.map(_.split(" ").map(_.toInt))
    val n = in.next.head
    val d = in.toList
    val es = (for (i <- (0 to n-2); j <- (i+1 to n-1) if d(i)(j) != -1) yield (i, j, d(i)(j))).sortBy(_._3)
    val t = (0 to n-1).toArray
    var cost = 0
    es.foreach(e => if (find(e._1, t) != find(e._2, t)) {unite(e._1, e._2, t); cost += e._3})
    println(cost)
}