import sys

import heapq

input = sys.stdin.readline





n = int(eval(input()))

Xs = []

Ys = []

for i in range(n):

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

    Xs.append((x-1, i))

    Ys.append((y-1, i))



Xs.sort()

Ys.sort()



edges = [[] for _ in range(n)]

for i in range(n-1):

    x, node = Xs[i]

    nx, new_node = Xs[i+1]

    edges[node].append((nx-x, new_node))

    edges[new_node].append((nx-x, node))



for i in range(n-1):

    y, node = Ys[i]

    ny, new_node = Ys[i+1]

    edges[node].append((ny-y, new_node))

    edges[new_node].append((ny-y, node))



seen = [False] * n

dist = [float('INF')] * n

todo = [(0, 0)]



while todo:

    d, node = heapq.heappop(todo)

    if seen[node]:

        continue

    seen[node] = True

    dist[node] = d



    for edge in edges[node]:

        nd, to = edge

        if dist[to] <= nd:

            continue

        heapq.heappush(todo, (nd, to))



print((sum(dist)))
import sys

from heapq import heappush, heappop

buf = sys.stdin.buffer





def main():

    n = int(buf.readline())

    XY = list(map(int, buf.read().split()))

    Xs = []

    Ys = []

    for i, (x, y) in enumerate(zip(XY, XY)):

        Xs.append((x-1, i))

        Ys.append((y-1, i))



    Xs.sort()

    Ys.sort()



    edges = [[] for _ in range(n)]

    for i, ((x, node), (nx, new_node)) in enumerate(zip(Xs[:-1], Xs[1:])):

        edges[node].append((nx-x, new_node))

        edges[new_node].append((nx-x, node))



    for i, ((y, node), (ny, new_node)) in enumerate(zip(Ys[:-1], Ys[1:])):

        edges[node].append((ny-y, new_node))

        edges[new_node].append((ny-y, node))



    seen = [False] * n

    dist = [float('INF')] * n

    todo = [(0, 0)]



    while todo:

        d, node = heappop(todo)

        if seen[node]:

            continue

        seen[node] = True

        dist[node] = d



        while edges[node]:

            edge = edges[node].pop()

            nd, to = edge

            if dist[to] <= nd:

                continue

            heappush(todo, (nd, to))



    print((sum(dist)))





if __name__ == "__main__":

    main()

import sys
import heapq

input = sys.stdin.readline
n = int(eval(input()))
Xs = []
Ys = []
for i in range(n):
    x, y = list(map(int, input().split()))
    Xs.append((x - 1, i))
    Ys.append((y - 1, i))
Xs.sort()
Ys.sort()
edges = [[] for _ in range(n)]
for i in range(n - 1):
    x, node = Xs[i]
    nx, new_node = Xs[i + 1]
    edges[node].append((nx - x, new_node))
    edges[new_node].append((nx - x, node))
for i in range(n - 1):
    y, node = Ys[i]
    ny, new_node = Ys[i + 1]
    edges[node].append((ny - y, new_node))
    edges[new_node].append((ny - y, node))
seen = [False] * n
dist = [float("INF")] * n
todo = [(0, 0)]
while todo:
    d, node = heapq.heappop(todo)
    if seen[node]:
        continue
    seen[node] = True
    dist[node] = d
    for edge in edges[node]:
        nd, to = edge
        if dist[to] <= nd:
            continue
        heapq.heappush(todo, (nd, to))
print((sum(dist)))

import sys
from heapq import heappush, heappop

buf = sys.stdin.buffer


def main():
    n = int(buf.readline())
    XY = list(map(int, buf.read().split()))
    Xs = []
    Ys = []
    for i, (x, y) in enumerate(zip(XY, XY)):
        Xs.append((x - 1, i))
        Ys.append((y - 1, i))
    Xs.sort()
    Ys.sort()
    edges = [[] for _ in range(n)]
    for i, ((x, node), (nx, new_node)) in enumerate(zip(Xs[:-1], Xs[1:])):
        edges[node].append((nx - x, new_node))
        edges[new_node].append((nx - x, node))
    for i, ((y, node), (ny, new_node)) in enumerate(zip(Ys[:-1], Ys[1:])):
        edges[node].append((ny - y, new_node))
        edges[new_node].append((ny - y, node))
    seen = [False] * n
    dist = [float("INF")] * n
    todo = [(0, 0)]
    while todo:
        d, node = heappop(todo)
        if seen[node]:
            continue
        seen[node] = True
        dist[node] = d
        while edges[node]:
            edge = edges[node].pop()
            nd, to = edge
            if dist[to] <= nd:
                continue
            heappush(todo, (nd, to))
    print((sum(dist)))


if __name__ == "__main__":
    main()

-import heapq +from heapq import heappush, heappop -input = sys.stdin.readline -n = int(eval(input())) -Xs = [] -Ys = [] -for i in range(n): -    x, y = list(map(int, input().split())) -    Xs.append((x - 1, i)) -    Ys.append((y - 1, i)) -Xs.sort() -Ys.sort() -edges = [[] for _ in range(n)] -for i in range(n - 1): -    x, node = Xs[i] -    nx, new_node = Xs[i + 1] -    edges[node].append((nx - x, new_node)) -    edges[new_node].append((nx - x, node)) -for i in range(n - 1): -    y, node = Ys[i] -    ny, new_node = Ys[i + 1] -    edges[node].append((ny - y, new_node)) -    edges[new_node].append((ny - y, node)) -seen = [False] * n -dist = [float("INF")] * n -todo = [(0, 0)] -while todo: -    d, node = heapq.heappop(todo) -    if seen[node]: -        continue -    seen[node] = True -    dist[node] = d -    for edge in edges[node]: -        nd, to = edge -        if dist[to] <= nd: +buf = sys.stdin.buffer + + +def main(): +    n = int(buf.readline()) +    XY = list(map(int, buf.read().split())) +    Xs = [] +    Ys = [] +    for i, (x, y) in enumerate(zip(XY, XY)): +        Xs.append((x - 1, i)) +        Ys.append((y - 1, i)) +    Xs.sort() +    Ys.sort() +    edges = [[] for _ in range(n)] +    for i, ((x, node), (nx, new_node)) in enumerate(zip(Xs[:-1], Xs[1:])): +        edges[node].append((nx - x, new_node)) +        edges[new_node].append((nx - x, node)) +    for i, ((y, node), (ny, new_node)) in enumerate(zip(Ys[:-1], Ys[1:])): +        edges[node].append((ny - y, new_node)) +        edges[new_node].append((ny - y, node)) +    seen = [False] * n +    dist = [float("INF")] * n +    todo = [(0, 0)] +    while todo: +        d, node = heappop(todo) +        if seen[node]: -        heapq.heappush(todo, (nd, to)) -print((sum(dist))) +        seen[node] = True +        dist[node] = d +        while edges[node]: +            edge = edges[node].pop() +            nd, to = edge +            if dist[to] <= nd: +                continue +            heappush(todo, (nd, to)) +    print((sum(dist))) + + +if __name__ == "__main__": +    main()