# Node
class Node
  attr_accessor :id, :edges, :cost, :done

  def initialize(id, edges = [], cost = nil, done = false)
    @id, @edges, @cost, @done = id, edges, cost, done
  end
end

# Edge
class Edge
  attr_accessor :to, :cost

  def initialize(to, cost)
    @to, @cost = to, cost
  end
end

# Prim
class Prim
  attr_accessor :nodes

  def initialize(data)
    @nodes = {}
    data.each do |id, edges|
      edges.map! { |edge| Edge.new(*edge) }
      @nodes[id] = Node.new(id, edges)
    end
  end

  def start(id)
    cost = 0

    @nodes.each do |id, node|
      node.done = false
    end

    node = @nodes[id]
    node.done = true
    queue = node.edges

    until queue.empty?
      queue.sort! { |l, r| r.cost <=> l.cost }
      edge = queue.pop
      done_node = @nodes[edge.to]
      next if done_node.done
      done_node.done = true
      cost += edge.cost
      queue += done_node.edges
    end

    cost
  end
end

data = {}

n = gets.chop.to_i

n.times do |i|
  data[i] = []
  gets.chop.split.each_with_index do |item, index|
    item = item.to_i
    next if item == -1
    data[i] << [index, item]
  end
end

prime = Prim.new data
puts prime.start 0