vs = $stdin.each_line.drop(1).map { |row| row.split(' ').map { |c| c.to_i } }.map.with_index do |row, i|
  [i, row[0], row[1]]
end
v_new = []
v_new.push(vs.pop)

e_cost_sum = 0
while !vs.empty? do
  v = vs.pop
  edge_min = v_new.map { |u| [(u[1] - v[1]).abs, (u[2] - v[2]).abs].min }.min
  v_new.push(v)
  e_cost_sum += edge_min
end

puts e_cost_sum