import java.util.*

data class Node(val id: Int, val edges: List<Edge>, var visited: Boolean = false)
data class Edge(val nodeId: Int, val weight: Int)

fun main(args: Array<String>) {
    val n = readLine()?.toInt() ?: 0
    val nodeList = List(n) { i ->
        val input = readLine()?.split(" ".toRegex())?.filterNot { it.isBlank() }?.map(String::toInt) ?: listOf()
        val edges = input.mapIndexed { index, w -> Edge(index, w) }.filter { it.weight != -1 }
        Node(i, edges)
    }
    var node = nodeList.first().apply { visited = true }
    val nextEdges = PriorityQueue<Edge>(compareBy { it.weight })
    nextEdges.addAll(node.edges)
    var weight = 0
    while (nextEdges.isNotEmpty()) {
        val nextEdge = nextEdges.poll()
        node = nodeList.first { it.id == nextEdge.nodeId }
        if (!node.visited) {
            weight += nextEdge.weight
            node.visited = true
            nextEdges.addAll(node.edges)
        }
    }
    println(weight)
}
