N=int(input())
x=[]
y=[]
for i in range(N):
  a,b=map(int,input().split())
  x+=[ a]
  y+=[ b]
cost=[[0 for i in range(N)] for j in range(N)]
for i in range(N):
  for j in range(N):
    cost[i][j]=min(abs(x[i]-x[j]),abs(y[i]-y[j]))
    cost[j][i]=cost[i][j]
used=[0]+[0]*(N-1)#0番目のみが入っている
mincost=[0]+[10**9]*(N-1)
res=0

while True:
  v=-1
  for u in range(N):
    if used[u]==0 and (v==-1 or mincost[u]<mincost[v]):
      v=u
  if v==-1:
    break
  used[v]=1
  res+=mincost[v]
  for u in range(N):
    mincost[u]=min(mincost[u],cost[v][u])
print(res)