import sys

input = sys.stdin.readline

n=int(eval(input()))

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

XY=[]

append=XY.append

for i in range(n):

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

  append([i,x,y])

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

for i in range(1,n):

  n1,x1,y1=XY[i-1][0],XY[i-1][1],XY[i-1][2]

  n2,x2,y2=XY[i][0],XY[i][1],XY[i][2]

  Edge[n1].append([min(abs(x2-x1),abs(y2-y1)),n2])

  Edge[n2].append([min(abs(x2-x1),abs(y2-y1)),n1])

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

for i in range(1,n):

  n1,x1,y1=XY[i-1][0],XY[i-1][1],XY[i-1][2]

  n2,x2,y2=XY[i][0],XY[i][1],XY[i][2]

  Edge[n1].append([min(abs(x2-x1),abs(y2-y1)),n2])

  Edge[n2].append([min(abs(x2-x1),abs(y2-y1)),n1])

import heapq

def prim_heap():

  used=[True]*n

  Edgelist=[]

  for e in Edge[0]:

    heapq.heappush(Edgelist,e)

  used[0]=False

  res=0

  while len(Edgelist)!=0:

    minedge=heapq.heappop(Edgelist)

    if not used[minedge[1]]:

      continue

    v=minedge[1]

    used[v]=False

    for e in Edge[v]:

      if used[e[1]]:

        heapq.heappush(Edgelist,e)

    res+=minedge[0]

  return res

print((prim_heap()))

import sys

sys.setrecursionlimit(1000000)

input = sys.stdin.readline



def MST(n, E):

    def par(m):

        if UF[m] == m:

            return m

        ret = par(UF[m])

        UF[m] = ret

        return ret

    

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

    E = sorted(E, key=lambda x: x[2])

    ans = 0

    for i, j, d in E:

        if par(i) != par(j):

            ans += d

            UF[par(i)] = par(j)

    return ans



N = int(eval(input()))



X = []

append=X.append

for i in range(N):

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

    append((i, x, y))



F = []

Y = sorted(X, key = lambda x: x[1])

for i in range(len(Y)-1):

    F.append((Y[i][0], Y[i+1][0], abs(Y[i+1][1]-Y[i][1])))



Y = sorted(X, key = lambda x: x[2])

for i in range(len(Y)-1):

    F.append((Y[i][0], Y[i+1][0], abs(Y[i+1][2]-Y[i][2])))



print((MST(N, F)))

import sys

input = sys.stdin.readline
n = int(eval(input()))
Edge = [[] for i in range(n)]
XY = []
append = XY.append
for i in range(n):
    x, y = list(map(int, input().split()))
    append([i, x, y])
XY.sort(key=lambda x: x[1])
for i in range(1, n):
    n1, x1, y1 = XY[i - 1][0], XY[i - 1][1], XY[i - 1][2]
    n2, x2, y2 = XY[i][0], XY[i][1], XY[i][2]
    Edge[n1].append([min(abs(x2 - x1), abs(y2 - y1)), n2])
    Edge[n2].append([min(abs(x2 - x1), abs(y2 - y1)), n1])
XY.sort(key=lambda x: x[2])
for i in range(1, n):
    n1, x1, y1 = XY[i - 1][0], XY[i - 1][1], XY[i - 1][2]
    n2, x2, y2 = XY[i][0], XY[i][1], XY[i][2]
    Edge[n1].append([min(abs(x2 - x1), abs(y2 - y1)), n2])
    Edge[n2].append([min(abs(x2 - x1), abs(y2 - y1)), n1])
import heapq


def prim_heap():
    used = [True] * n
    Edgelist = []
    for e in Edge[0]:
        heapq.heappush(Edgelist, e)
    used[0] = False
    res = 0
    while len(Edgelist) != 0:
        minedge = heapq.heappop(Edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        used[v] = False
        for e in Edge[v]:
            if used[e[1]]:
                heapq.heappush(Edgelist, e)
        res += minedge[0]
    return res


print((prim_heap()))

import sys

sys.setrecursionlimit(1000000)
input = sys.stdin.readline


def MST(n, E):
    def par(m):
        if UF[m] == m:
            return m
        ret = par(UF[m])
        UF[m] = ret
        return ret

    UF = [i for i in range(n)]
    E = sorted(E, key=lambda x: x[2])
    ans = 0
    for i, j, d in E:
        if par(i) != par(j):
            ans += d
            UF[par(i)] = par(j)
    return ans


N = int(eval(input()))
X = []
append = X.append
for i in range(N):
    x, y = list(map(int, input().split()))
    append((i, x, y))
F = []
Y = sorted(X, key=lambda x: x[1])
for i in range(len(Y) - 1):
    F.append((Y[i][0], Y[i + 1][0], abs(Y[i + 1][1] - Y[i][1])))
Y = sorted(X, key=lambda x: x[2])
for i in range(len(Y) - 1):
    F.append((Y[i][0], Y[i + 1][0], abs(Y[i + 1][2] - Y[i][2])))
print((MST(N, F)))

+sys.setrecursionlimit(1000000) -n = int(eval(input())) -Edge = [[] for i in range(n)] -XY = [] -append = XY.append -for i in range(n): -    x, y = list(map(int, input().split())) -    append([i, x, y]) -XY.sort(key=lambda x: x[1]) -for i in range(1, n): -    n1, x1, y1 = XY[i - 1][0], XY[i - 1][1], XY[i - 1][2] -    n2, x2, y2 = XY[i][0], XY[i][1], XY[i][2] -    Edge[n1].append([min(abs(x2 - x1), abs(y2 - y1)), n2]) -    Edge[n2].append([min(abs(x2 - x1), abs(y2 - y1)), n1]) -XY.sort(key=lambda x: x[2]) -for i in range(1, n): -    n1, x1, y1 = XY[i - 1][0], XY[i - 1][1], XY[i - 1][2] -    n2, x2, y2 = XY[i][0], XY[i][1], XY[i][2] -    Edge[n1].append([min(abs(x2 - x1), abs(y2 - y1)), n2]) -    Edge[n2].append([min(abs(x2 - x1), abs(y2 - y1)), n1]) -import heapq -def prim_heap(): -    used = [True] * n -    Edgelist = [] -    for e in Edge[0]: -        heapq.heappush(Edgelist, e) -    used[0] = False -    res = 0 -    while len(Edgelist) != 0: -        minedge = heapq.heappop(Edgelist) -        if not used[minedge[1]]: -            continue -        v = minedge[1] -        used[v] = False -        for e in Edge[v]: -            if used[e[1]]: -                heapq.heappush(Edgelist, e) -        res += minedge[0] -    return res +def MST(n, E): +    def par(m): +        if UF[m] == m: +            return m +        ret = par(UF[m]) +        UF[m] = ret +        return ret + +    UF = [i for i in range(n)] +    E = sorted(E, key=lambda x: x[2]) +    ans = 0 +    for i, j, d in E: +        if par(i) != par(j): +            ans += d +            UF[par(i)] = par(j) +    return ans -print((prim_heap())) +N = int(eval(input())) +X = [] +append = X.append +for i in range(N): +    x, y = list(map(int, input().split())) +    append((i, x, y)) +F = [] +Y = sorted(X, key=lambda x: x[1]) +for i in range(len(Y) - 1): +    F.append((Y[i][0], Y[i + 1][0], abs(Y[i + 1][1] - Y[i][1]))) +Y = sorted(X, key=lambda x: x[2]) +for i in range(len(Y) - 1): +    F.append((Y[i][0], Y[i + 1][0], abs(Y[i + 1][2] - Y[i][2]))) +print((MST(N, F)))