import numpy as np
from heapq import *

N=int(input())
pos_mat=np.zeros((N,2))
for i in range(N):
    buf=input().split()
    pos_mat[i,0]=int(buf[0])
    pos_mat[i,1]=int(buf[1])
    
cost_mat=np.zeros((N,N))
for u in range(N):
    for v in range(u+1, N):
        cost_mat[u,v]=min(abs(pos_mat[u, 0]-pos_mat[v,0]), abs(pos_mat[u, 1]-pos_mat[v,1]))

minCost=[float("inf") for _ in range(N)]
minCost[0]=0
totalCost=0

h=[]#list of (cost, vertex ID)
heappush(h, (0, 0))
while(len(h)>0):
    cost, u=heappop(h)
    if cost>minCost[u]:
        continue
    totalCost+=cost
    for v in range(N):
        edgeCost=cost_mat[min(u,v), max(u,v)]
        if (minCost[v]>edgeCost):
            minCost[v]=edgeCost
            heappush(h,(minCost[v],v))
            
print(int(totalCost))