def mst ms, n
  colors = Array.new(n){1} # 1:white, 2:gray, 3:black
  costs = Array.new(n){Float::INFINITY}
  sums = 0

  colors[0] = 2
  costs[0] = 0

  while true do
    min_cost = Float::INFINITY
    min_cost_i = nil
    costs.each_with_index do |cost, i|
      if colors[i] == 2 && cost < min_cost
        min_cost = cost
        min_cost_i = i
      end
    end

    break if min_cost_i.nil?

    colors[min_cost_i] = 3
    sums += min_cost

    colors.each_with_index do |color, i|
      if color == 3
        ms[i].each_with_index do |m, j|
          if colors[j] != 3 && m < costs[j]
            costs[j] = m
            colors[j] = 2
          end
        end
      end
    end
  end

  sums
end

n = gets.chomp.to_i
ms = Array.new(n)
n.times do |i|
  ms[i] = gets.chomp.strip.split(' ').map(&:to_i).map{|x| x == -1 ? Float::INFINITY : x}
end
puts mst(ms, n)
