import heapq

import sys

input = sys.stdin.readline



def main():

    N = int(eval(input()))

    x,y = [],[]

    root = [[] for _ in range(N)]

    go = [True for _ in range(N)]

    for i in range(N):

        _x,_y = list(map(int,input().split()))

        x.append((_x,i))

        y.append((_y,i))

        

    x.sort()

    y.sort()

    

    for i in range(N-1):

        dx = x[i+1][0] - x[i][0]

        dy = y[i+1][0] - y[i][0]

        root[x[i][1]].append((dx,x[i+1][1]))

        root[x[i+1][1]].append((dx,x[i][1]))

        root[y[i][1]].append((dy,y[i+1][1]))

        root[y[i+1][1]].append((dy,y[i][1]))

    

    ans = 0    

    queue = root[0]

    heapq.heapify(queue)

    go[0] = False

    while queue:

        cost,following = heapq.heappop(queue)

        if not go[following]:

            continue

        go[following] = False

        for e in root[following]:

            if go[e[1]]:

                heapq.heappush(queue,e)

        ans += cost

        

    print(ans)

    

if __name__ == "__main__":

    main()
import sys

input = sys.stdin.readline



def main():

    N = int(eval(input()))

    rx,ry = [],[]

    _root = []

    for i in range(N):

        a,b = list(map(int,input().split()))

        rx.append((a,i))

        ry.append((b,i))

    

    I = [i for i in range(N)]

    rank = [1 for _ in range(N)]

    def root(x):

        if x == I[x]:

            return x

        I[x] = root(I[x])

        return I[x]

    

    def unite(x,y):

        px,py = root(x),root(y)

        rx,ry = rank[px],rank[py]

        if rx > ry:

            I[py] = px

        else:

            I[px] = py

            if rx == ry:

                rank[py] += 1



    ans = 0

    rx.sort()

    ry.sort()

    for i in range(N-1):

        dx = rx[i+1][0]-rx[i][0]

        dy = ry[i+1][0]-ry[i][0]

        _root.append((dx,rx[i][1],rx[i+1][1]))

        _root.append((dy,ry[i][1],ry[i+1][1]))

        

    _root.sort()

    a = len(_root)

    for i in range(a):

        cost,now,fol = _root[i]

        if (root(now) == root(fol)):

            continue

        ans += cost

        unite(now,fol)

        

    print(ans)

    

if __name__ == "__main__":

    main()
import heapq
import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    x, y = [], []
    root = [[] for _ in range(N)]
    go = [True for _ in range(N)]
    for i in range(N):
        _x, _y = list(map(int, input().split()))
        x.append((_x, i))
        y.append((_y, i))
    x.sort()
    y.sort()
    for i in range(N - 1):
        dx = x[i + 1][0] - x[i][0]
        dy = y[i + 1][0] - y[i][0]
        root[x[i][1]].append((dx, x[i + 1][1]))
        root[x[i + 1][1]].append((dx, x[i][1]))
        root[y[i][1]].append((dy, y[i + 1][1]))
        root[y[i + 1][1]].append((dy, y[i][1]))
    ans = 0
    queue = root[0]
    heapq.heapify(queue)
    go[0] = False
    while queue:
        cost, following = heapq.heappop(queue)
        if not go[following]:
            continue
        go[following] = False
        for e in root[following]:
            if go[e[1]]:
                heapq.heappush(queue, e)
        ans += cost
    print(ans)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    rx, ry = [], []
    _root = []
    for i in range(N):
        a, b = list(map(int, input().split()))
        rx.append((a, i))
        ry.append((b, i))
    I = [i for i in range(N)]
    rank = [1 for _ in range(N)]

    def root(x):
        if x == I[x]:
            return x
        I[x] = root(I[x])
        return I[x]

    def unite(x, y):
        px, py = root(x), root(y)
        rx, ry = rank[px], rank[py]
        if rx > ry:
            I[py] = px
        else:
            I[px] = py
            if rx == ry:
                rank[py] += 1

    ans = 0
    rx.sort()
    ry.sort()
    for i in range(N - 1):
        dx = rx[i + 1][0] - rx[i][0]
        dy = ry[i + 1][0] - ry[i][0]
        _root.append((dx, rx[i][1], rx[i + 1][1]))
        _root.append((dy, ry[i][1], ry[i + 1][1]))
    _root.sort()
    a = len(_root)
    for i in range(a):
        cost, now, fol = _root[i]
        if root(now) == root(fol):
            continue
        ans += cost
        unite(now, fol)
    print(ans)


if __name__ == "__main__":
    main()

-import heapq -    x, y = [], [] -    root = [[] for _ in range(N)] -    go = [True for _ in range(N)] +    rx, ry = [], [] +    _root = [] -        _x, _y = list(map(int, input().split())) -        x.append((_x, i)) -        y.append((_y, i)) -    x.sort() -    y.sort() +        a, b = list(map(int, input().split())) +        rx.append((a, i)) +        ry.append((b, i)) +    I = [i for i in range(N)] +    rank = [1 for _ in range(N)] + +    def root(x): +        if x == I[x]: +            return x +        I[x] = root(I[x]) +        return I[x] + +    def unite(x, y): +        px, py = root(x), root(y) +        rx, ry = rank[px], rank[py] +        if rx > ry: +            I[py] = px +        else: +            I[px] = py +            if rx == ry: +                rank[py] += 1 + +    ans = 0 +    rx.sort() +    ry.sort() -        dx = x[i + 1][0] - x[i][0] -        dy = y[i + 1][0] - y[i][0] -        root[x[i][1]].append((dx, x[i + 1][1])) -        root[x[i + 1][1]].append((dx, x[i][1])) -        root[y[i][1]].append((dy, y[i + 1][1])) -        root[y[i + 1][1]].append((dy, y[i][1])) -    ans = 0 -    queue = root[0] -    heapq.heapify(queue) -    go[0] = False -    while queue: -        cost, following = heapq.heappop(queue) -        if not go[following]: +        dx = rx[i + 1][0] - rx[i][0] +        dy = ry[i + 1][0] - ry[i][0] +        _root.append((dx, rx[i][1], rx[i + 1][1])) +        _root.append((dy, ry[i][1], ry[i + 1][1])) +    _root.sort() +    a = len(_root) +    for i in range(a): +        cost, now, fol = _root[i] +        if root(now) == root(fol): -        go[following] = False -        for e in root[following]: -            if go[e[1]]: -                heapq.heappush(queue, e) +        unite(now, fol)