import heapq



n=int(eval(input()))

pts=[list(map(int,input().split())) for _ in range(n)]



x=sorted([(i,pts[i][0]) for i in range(n)],key=lambda x:x[1])

y=sorted([(i,pts[i][1]) for i in range(n)],key=lambda x:x[1])



invx=[0]*n

invy=[0]*n



for i in range(n):

    cx,_=x[i]

    invx[cx]=i

    cy,_=y[i]

    invy[cy]=i



xi=invx[0]

yi=invy[0]



q=[(0,0)]





visit=[0]*n

ans=0



while q:

    cnt,i=heapq.heappop(q)



    if visit[i]==1:

        continue



    visit[i]=1

    ans+=cnt



    xi=invx[i]

    yi=invy[i]



    if xi>0 and visit[x[xi-1][0]]==0:

        heapq.heappush(q,(x[xi][1]-x[xi-1][1],x[xi-1][0]))

    if xi<n-1 and visit[x[xi+1][0]]==0:

        heapq.heappush(q,(x[xi+1][1]-x[xi][1],x[xi+1][0]))



    if yi>0 and visit[y[yi-1][0]]==0:

        heapq.heappush(q,(y[yi][1]-y[yi-1][1],y[yi-1][0]))

    if yi<n-1 and visit[y[yi+1][0]]==0:

        heapq.heappush(q,(y[yi+1][1]-y[yi][1],y[yi+1][0]))

    

print(ans)
import heapq



n=int(eval(input()))

pts=[list(map(int,input().split())) for _ in range(n)]



x=sorted([(i,pts[i][0]) for i in range(n)],key=lambda x:x[1])

y=sorted([(i,pts[i][1]) for i in range(n)],key=lambda x:x[1])



invx=[0]*n

invy=[0]*n



for i in range(n):

    invx[x[i][0]]=i

    invy[y[i][0]]=i



q=[(0,0)]



visit=[0]*n

ans=0



while q:

    cnt,i=heapq.heappop(q)



    if visit[i]==1:

        continue



    visit[i]=1

    ans+=cnt



    xi=invx[i]

    yi=invy[i]



    for delta in [-1,1]:

        nx=xi+delta

        ny=yi+delta

        if 0<=nx<n and visit[x[nx][0]]==0:

            heapq.heappush(q,(abs(x[nx][1]-x[xi][1]),x[nx][0]))

        if 0<=ny<n and visit[y[ny][0]]==0:

            heapq.heappush(q,(abs(y[ny][1]-y[yi][1]),y[ny][0]))



print(ans)
import heapq

n = int(eval(input()))
pts = [list(map(int, input().split())) for _ in range(n)]
x = sorted([(i, pts[i][0]) for i in range(n)], key=lambda x: x[1])
y = sorted([(i, pts[i][1]) for i in range(n)], key=lambda x: x[1])
invx = [0] * n
invy = [0] * n
for i in range(n):
    cx, _ = x[i]
    invx[cx] = i
    cy, _ = y[i]
    invy[cy] = i
xi = invx[0]
yi = invy[0]
q = [(0, 0)]
visit = [0] * n
ans = 0
while q:
    cnt, i = heapq.heappop(q)
    if visit[i] == 1:
        continue
    visit[i] = 1
    ans += cnt
    xi = invx[i]
    yi = invy[i]
    if xi > 0 and visit[x[xi - 1][0]] == 0:
        heapq.heappush(q, (x[xi][1] - x[xi - 1][1], x[xi - 1][0]))
    if xi < n - 1 and visit[x[xi + 1][0]] == 0:
        heapq.heappush(q, (x[xi + 1][1] - x[xi][1], x[xi + 1][0]))
    if yi > 0 and visit[y[yi - 1][0]] == 0:
        heapq.heappush(q, (y[yi][1] - y[yi - 1][1], y[yi - 1][0]))
    if yi < n - 1 and visit[y[yi + 1][0]] == 0:
        heapq.heappush(q, (y[yi + 1][1] - y[yi][1], y[yi + 1][0]))
print(ans)

import heapq

n = int(eval(input()))
pts = [list(map(int, input().split())) for _ in range(n)]
x = sorted([(i, pts[i][0]) for i in range(n)], key=lambda x: x[1])
y = sorted([(i, pts[i][1]) for i in range(n)], key=lambda x: x[1])
invx = [0] * n
invy = [0] * n
for i in range(n):
    invx[x[i][0]] = i
    invy[y[i][0]] = i
q = [(0, 0)]
visit = [0] * n
ans = 0
while q:
    cnt, i = heapq.heappop(q)
    if visit[i] == 1:
        continue
    visit[i] = 1
    ans += cnt
    xi = invx[i]
    yi = invy[i]
    for delta in [-1, 1]:
        nx = xi + delta
        ny = yi + delta
        if 0 <= nx < n and visit[x[nx][0]] == 0:
            heapq.heappush(q, (abs(x[nx][1] - x[xi][1]), x[nx][0]))
        if 0 <= ny < n and visit[y[ny][0]] == 0:
            heapq.heappush(q, (abs(y[ny][1] - y[yi][1]), y[ny][0]))
print(ans)

-    cx, _ = x[i] -    invx[cx] = i -    cy, _ = y[i] -    invy[cy] = i -xi = invx[0] -yi = invy[0] +    invx[x[i][0]] = i +    invy[y[i][0]] = i -    if xi > 0 and visit[x[xi - 1][0]] == 0: -        heapq.heappush(q, (x[xi][1] - x[xi - 1][1], x[xi - 1][0])) -    if xi < n - 1 and visit[x[xi + 1][0]] == 0: -        heapq.heappush(q, (x[xi + 1][1] - x[xi][1], x[xi + 1][0])) -    if yi > 0 and visit[y[yi - 1][0]] == 0: -        heapq.heappush(q, (y[yi][1] - y[yi - 1][1], y[yi - 1][0])) -    if yi < n - 1 and visit[y[yi + 1][0]] == 0: -        heapq.heappush(q, (y[yi + 1][1] - y[yi][1], y[yi + 1][0])) +    for delta in [-1, 1]: +        nx = xi + delta +        ny = yi + delta +        if 0 <= nx < n and visit[x[nx][0]] == 0: +            heapq.heappush(q, (abs(x[nx][1] - x[xi][1]), x[nx][0])) +        if 0 <= ny < n and visit[y[ny][0]] == 0: +            heapq.heappush(q, (abs(y[ny][1] - y[yi][1]), y[ny][0]))