N=int(eval(input()))

city=[0]*N

for i in range(N):

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

    city[i]=(i,x,y)

city.sort(key=lambda x:x[1])

data=[(city[i][0],city[i+1][0],city[i+1][1]-city[i][1]) for i in range(N-1)]

city.sort(key=lambda x:x[2])

data+=[(city[i][0],city[i+1][0],city[i+1][2]-city[i][2]) for i in range(N-1)]

data.sort(key=lambda x:x[2])

root=[-1]*N

def search(i):

    global root

    if root[i]>=0:

        root[i]=search(root[i])

        return root[i]

    return i

cost=0

for i,j,k in data:

    ri=search(i)

    rj=search(j)

    if ri==rj:

        continue

    elif root[ri]<=root[rj]:

        cost+=k

        root[ri]+=root[rj]

        root[rj]=ri

    elif root[ri]>root[rj]:

        cost+=k

        root[rj]+=root[ri]

        root[ri]=rj

print(cost)
import sys

input=sys.stdin.readline

N=int(eval(input()))

P=[tuple(map(int,input().split())) for i in range(N)]

X=[(i,P[i][0]) for i in range(N)]

Y=[(i,P[i][1]) for i in range(N)]

X.sort(key=lambda x:x[1])

Y.sort(key=lambda x:x[1])

Z=[(X[i][0],X[i+1][0],X[i+1][1]-X[i][1]) for i in range(N-1)]+[(Y[i][0],Y[i+1][0],Y[i+1][1]-Y[i][1]) for i in range(N-1)]

Z.sort(key=lambda x:x[2])

root=[-1]*N

def search(i):

    global root

    if root[i]>=0:

        root[i]=search(root[i])

        return root[i]

    return i

ans=0

for i,j,k in Z:

    ri=search(i)

    rj=search(j)

    if ri==rj:

        continue

    elif root[ri]<=root[rj]:

        root[ri]+=root[rj]

        root[rj]=ri

        ans+=k

    else:

        root[rj]+=root[ri]

        root[ri]=rj

        ans+=k

print(ans)
N = int(eval(input()))
city = [0] * N
for i in range(N):
    x, y = list(map(int, input().split()))
    city[i] = (i, x, y)
city.sort(key=lambda x: x[1])
data = [(city[i][0], city[i + 1][0], city[i + 1][1] - city[i][1]) for i in range(N - 1)]
city.sort(key=lambda x: x[2])
data += [
    (city[i][0], city[i + 1][0], city[i + 1][2] - city[i][2]) for i in range(N - 1)
]
data.sort(key=lambda x: x[2])
root = [-1] * N


def search(i):
    global root
    if root[i] >= 0:
        root[i] = search(root[i])
        return root[i]
    return i


cost = 0
for i, j, k in data:
    ri = search(i)
    rj = search(j)
    if ri == rj:
        continue
    elif root[ri] <= root[rj]:
        cost += k
        root[ri] += root[rj]
        root[rj] = ri
    elif root[ri] > root[rj]:
        cost += k
        root[rj] += root[ri]
        root[ri] = rj
print(cost)

import sys

input = sys.stdin.readline
N = int(eval(input()))
P = [tuple(map(int, input().split())) for i in range(N)]
X = [(i, P[i][0]) for i in range(N)]
Y = [(i, P[i][1]) for i in range(N)]
X.sort(key=lambda x: x[1])
Y.sort(key=lambda x: x[1])
Z = [(X[i][0], X[i + 1][0], X[i + 1][1] - X[i][1]) for i in range(N - 1)] + [
    (Y[i][0], Y[i + 1][0], Y[i + 1][1] - Y[i][1]) for i in range(N - 1)
]
Z.sort(key=lambda x: x[2])
root = [-1] * N


def search(i):
    global root
    if root[i] >= 0:
        root[i] = search(root[i])
        return root[i]
    return i


ans = 0
for i, j, k in Z:
    ri = search(i)
    rj = search(j)
    if ri == rj:
        continue
    elif root[ri] <= root[rj]:
        root[ri] += root[rj]
        root[rj] = ri
        ans += k
    else:
        root[rj] += root[ri]
        root[ri] = rj
        ans += k
print(ans)

+import sys + +input = sys.stdin.readline -city = [0] * N -for i in range(N): -    x, y = list(map(int, input().split())) -    city[i] = (i, x, y) -city.sort(key=lambda x: x[1]) -data = [(city[i][0], city[i + 1][0], city[i + 1][1] - city[i][1]) for i in range(N - 1)] -city.sort(key=lambda x: x[2]) -data += [ -    (city[i][0], city[i + 1][0], city[i + 1][2] - city[i][2]) for i in range(N - 1) +P = [tuple(map(int, input().split())) for i in range(N)] +X = [(i, P[i][0]) for i in range(N)] +Y = [(i, P[i][1]) for i in range(N)] +X.sort(key=lambda x: x[1]) +Y.sort(key=lambda x: x[1]) +Z = [(X[i][0], X[i + 1][0], X[i + 1][1] - X[i][1]) for i in range(N - 1)] + [ +    (Y[i][0], Y[i + 1][0], Y[i + 1][1] - Y[i][1]) for i in range(N - 1) -data.sort(key=lambda x: x[2]) +Z.sort(key=lambda x: x[2]) -cost = 0 -for i, j, k in data: +ans = 0 +for i, j, k in Z: -        cost += k -    elif root[ri] > root[rj]: -        cost += k +        ans += k +    else: -print(cost) +        ans += k +print(ans)