class DisjointSet
  def initialize(size = nil)
    @rank = Array.new(size){0}
    @parent = []
    if size
      size.times do |i|
        @parent[i] = i
      end
    end
  end

  def same(x, y)
    return find(x) == find(y)
  end

  def unite(x, y)
    x = find(x)
    y = find(y)
    if @rank[x] > @rank[y]
      @parent[y] = x
    else
      @parent[x] = y
      @rank[y] += 1 if @rank[x] == @rank[y]
    end
  end

  def find(x)
    @parent[x] = find(@parent[x]) unless x == @parent[x] 
    return @parent[x]
  end
end

n = gets.to_i
tree = DisjointSet.new(n)

sides = {}
n.times{|i|
  arr = gets.split.map(&:to_i)
  arr.each_with_index{|val, j|
    if i > j && val > -1
      sides["#{i} #{j}"] = val
    end
  }
}

sides = sides.sort_by {|k, v| v}
sum = 0
sides.each{|side|
  r, l = side[0].split.map(&:to_i)
  unless tree.same(r, l) then
    sum += side[1]
    tree.unite(r, l)
  end
}
puts sum