import sys

input=sys.stdin.readline

n=int(eval(input()))

pointx=[]

pointy=[]

for i in range(n):

    x,y=list(map(int,input().split()))

    pointx.append((x,i))

    pointy.append((y,i))

pointx.sort()

pointy.sort()

G=[[] for i in range(n)]

for i in range(n-1):

    dx=abs(pointx[i+1][0]-pointx[i][0])

    G[pointx[i][1]].append((dx,pointx[i+1][1]))

    G[pointx[i+1][1]].append((dx,pointx[i][1]))

    dy=abs(pointy[i+1][0]-pointy[i][0])

    G[pointy[i][1]].append((dy,pointy[i+1][1]))

    G[pointy[i+1][1]].append((dy,pointy[i][1]))

import heapq

q=[]

check=[False]*n

check[0]=True

for i in G[0]:

    heapq.heappush(q,i)

ans=0

while len(q)>0:

    now=heapq.heappop(q)

    if not check[now[1]]:

        ans+=now[0]

        check[now[1]]=True

        for i in G[now[1]]:

            heapq.heappush(q,i)

print(ans)





import sys

input=sys.stdin.readline

n=int(eval(input()))

pointx=[]

pointy=[]

for i in range(n):

    x,y=list(map(int,input().split()))

    pointx.append((x,i))

    pointy.append((y,i))

pointx.sort()

pointy.sort()

G=[[] for i in range(n)]

for i in range(n-1):

    dx=abs(pointx[i+1][0]-pointx[i][0])

    G[pointx[i][1]].append((dx,pointx[i+1][1]))

    G[pointx[i+1][1]].append((dx,pointx[i][1]))

    dy=abs(pointy[i+1][0]-pointy[i][0])

    G[pointy[i][1]].append((dy,pointy[i+1][1]))

    G[pointy[i+1][1]].append((dy,pointy[i][1]))

import heapq

q=[]

check=[False]*n

INF=10**10

d=[INF]*n

ans=0

d[0]=0

heapq.heappush(q,(0,0))

while len(q)>0:

    now=heapq.heappop(q)

    if not check[now[1]]:

        ans+=now[0]

        check[now[1]]=True

        for i in G[now[1]]:

            if i[0]<d[i[1]]:

                d[i[1]]=i[0]

                heapq.heappush(q,i)

print(ans)





import sys

input = sys.stdin.readline
n = int(eval(input()))
pointx = []
pointy = []
for i in range(n):
    x, y = list(map(int, input().split()))
    pointx.append((x, i))
    pointy.append((y, i))
pointx.sort()
pointy.sort()
G = [[] for i in range(n)]
for i in range(n - 1):
    dx = abs(pointx[i + 1][0] - pointx[i][0])
    G[pointx[i][1]].append((dx, pointx[i + 1][1]))
    G[pointx[i + 1][1]].append((dx, pointx[i][1]))
    dy = abs(pointy[i + 1][0] - pointy[i][0])
    G[pointy[i][1]].append((dy, pointy[i + 1][1]))
    G[pointy[i + 1][1]].append((dy, pointy[i][1]))
import heapq

q = []
check = [False] * n
check[0] = True
for i in G[0]:
    heapq.heappush(q, i)
ans = 0
while len(q) > 0:
    now = heapq.heappop(q)
    if not check[now[1]]:
        ans += now[0]
        check[now[1]] = True
        for i in G[now[1]]:
            heapq.heappush(q, i)
print(ans)

import sys

input = sys.stdin.readline
n = int(eval(input()))
pointx = []
pointy = []
for i in range(n):
    x, y = list(map(int, input().split()))
    pointx.append((x, i))
    pointy.append((y, i))
pointx.sort()
pointy.sort()
G = [[] for i in range(n)]
for i in range(n - 1):
    dx = abs(pointx[i + 1][0] - pointx[i][0])
    G[pointx[i][1]].append((dx, pointx[i + 1][1]))
    G[pointx[i + 1][1]].append((dx, pointx[i][1]))
    dy = abs(pointy[i + 1][0] - pointy[i][0])
    G[pointy[i][1]].append((dy, pointy[i + 1][1]))
    G[pointy[i + 1][1]].append((dy, pointy[i][1]))
import heapq

q = []
check = [False] * n
INF = 10**10
d = [INF] * n
ans = 0
d[0] = 0
heapq.heappush(q, (0, 0))
while len(q) > 0:
    now = heapq.heappop(q)
    if not check[now[1]]:
        ans += now[0]
        check[now[1]] = True
        for i in G[now[1]]:
            if i[0] < d[i[1]]:
                d[i[1]] = i[0]
                heapq.heappush(q, i)
print(ans)

-check[0] = True -for i in G[0]: -    heapq.heappush(q, i) +INF = 10**10 +d = [INF] * n +d[0] = 0 +heapq.heappush(q, (0, 0)) -            heapq.heappush(q, i) +            if i[0] < d[i[1]]: +                d[i[1]] = i[0] +                heapq.heappush(q, i)