def prim(s)
	mst = [s]
	sum = 0
	while mst.size != $n
		list = []
		mst.each do |u|
			list.push($m[u].map.with_index{|val,ix|mst.include?(ix) ? Float::INFINITY : val})
		end
		min = Float::INFINITY; min_i = nil
		list.each do |wv|
			wv.each_with_index do |w,i|
				if w < min
					min = w; min_i = i;
				end
			end
		end
		mst.push(min_i)
		sum += min
	end
	return sum
end

$n = gets.chomp.to_i
$m = []
$n.times do |i|
	$m[i] = gets.chomp.split(" ").map{|val|val.to_i == -1 ? Float::INFINITY : val.to_i}
end

puts prim(0)
