n=gets.to_i
a=[]
n.times do |i|
  a<< gets.split.map(&:to_i)+[i]
end
b=a.sort{|x,y|x[0]<=>y[0]}
c=a.sort{|x,y|x[1]<=>y[1]}
r=[10**9]*n
n.times do |i|
  if i != 0
    r[b[i][2]] = [r[b[i][2]],b[i][0]-b[i-1][0]].min
    r[c[i][2]] = [r[c[i][2]],c[i][1]-c[i-1][1]].min
  end
  if i != n-1
    r[b[i][2]] = [r[b[i][2]],b[i+1][0]-b[i][0]].min
    r[c[i][2]] = [r[c[i][2]],c[i+1][1]-c[i][1]].min
  end
end
p r.inject(:+)-r.min