def prim
  visited = [0]
  length = []

  until visited.length == @input_array.length do
    min_len, min_point = Float::INFINITY, 0
    visited.each do |x|
      @input_array[x].each_with_index do |y, i|
        if !visited.include?(i) && y != -1
          min_len, min_point = y , i if min_len > y
        end
      end
    end
    length << min_len
    visited << min_point
  end
  length.inject(:+)
end

@input_array = []

gets.to_i.times do
  @input_array << gets.split(" ").map{|x| x.to_i}
end

puts prim

