N = int(eval(input()))

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

z.sort()

y = [(z[i][1],i) for i in range(N)]

y.sort()

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

for i in range(N-1):

    j = y[i][1]

    cost[i].append((i-1,min(abs(z[i][0]-z[i-1][0]),abs(z[i][1]-z[i-1][1]))))

    j_ = y[i-1][1]

    cost[j].append((j_,min(abs(y[i][0]-y[i-1][0]),abs(z[j][0]-z[j_][0]))))

    cost[i].append((i+1,min(abs(z[i+1][0]-z[i][0]),abs(z[i+1][1]-z[i][1]))))

    j_ = y[i+1][1]

    cost[j].append((j_,min(abs(y[i+1][0]-y[i][0]),abs(z[j_][1]-z[j][1]))))



i = N-1

j = y[i][1]

cost[i].append((i-1,min(abs(z[i][0]-z[i-1][0]),abs(z[i][1]-z[i-1][1]))))

j_ = y[i-1][1]

cost[j].append((j_,min(abs(y[i][0]-y[i-1][0]),abs(z[j][0]-z[j_][0]))))





import heapq



def prim(cost):

    V = len(cost)

    res = 0

    mincost = [float('inf')]*V

    used = [False]*V

    hq = []

    mincost[0] = 0

    heapq.heappush(hq,(0,0))

    while hq:

        weight,u = heapq.heappop(hq)

        if not used[u]:

            used[u] = True

            res += weight

            mincost[u] = weight

            for v, v_cost in cost[u]:

                if not used[v]:

                    heapq.heappush(hq,(v_cost,v))

    return res



print((prim(cost)))
import heapq



def prim(cost):

    V = len(cost)

    res = 0

    mincost = [float('inf')]*V

    used = [False]*V

    hq = []

    mincost[0] = 0

    heapq.heappush(hq,(0,0))

    while hq:

        weight,u = heapq.heappop(hq)

        if used[u]:

            continue

        used[u] = True

        res += weight

        mincost[u] = weight

        for v, v_cost in cost[u]:

            if not used[v]:

                heapq.heappush(hq,(v_cost,v))

    return res



N = int(eval(input()))

xyi = []

for i in range(N):

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

    xyi.append([x,y,i])



xyi.sort()

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

for i in range(N-1):

    xj = xyi[i][0]

    j = xyi[i][2]

    xk = xyi[i+1][0]

    k = xyi[i+1][2]

    cost[j].append((k,xk-xj))

    cost[k].append((j,xk-xj))

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

for i in range(N-1):

    yj = xyi[i][1]

    j = xyi[i][2]

    yk = xyi[i+1][1]

    k = xyi[i+1][2]

    cost[j].append((k,yk-yj))

    cost[k].append((j,yk-yj))



print((prim(cost)))

N = int(eval(input()))
z = [tuple(map(int, input().split())) for i in range(N)]
z.sort()
y = [(z[i][1], i) for i in range(N)]
y.sort()
cost = [[] for i in range(N)]
for i in range(N - 1):
    j = y[i][1]
    cost[i].append((i - 1, min(abs(z[i][0] - z[i - 1][0]), abs(z[i][1] - z[i - 1][1]))))
    j_ = y[i - 1][1]
    cost[j].append((j_, min(abs(y[i][0] - y[i - 1][0]), abs(z[j][0] - z[j_][0]))))
    cost[i].append((i + 1, min(abs(z[i + 1][0] - z[i][0]), abs(z[i + 1][1] - z[i][1]))))
    j_ = y[i + 1][1]
    cost[j].append((j_, min(abs(y[i + 1][0] - y[i][0]), abs(z[j_][1] - z[j][1]))))
i = N - 1
j = y[i][1]
cost[i].append((i - 1, min(abs(z[i][0] - z[i - 1][0]), abs(z[i][1] - z[i - 1][1]))))
j_ = y[i - 1][1]
cost[j].append((j_, min(abs(y[i][0] - y[i - 1][0]), abs(z[j][0] - z[j_][0]))))
import heapq


def prim(cost):
    V = len(cost)
    res = 0
    mincost = [float("inf")] * V
    used = [False] * V
    hq = []
    mincost[0] = 0
    heapq.heappush(hq, (0, 0))
    while hq:
        weight, u = heapq.heappop(hq)
        if not used[u]:
            used[u] = True
            res += weight
            mincost[u] = weight
            for v, v_cost in cost[u]:
                if not used[v]:
                    heapq.heappush(hq, (v_cost, v))
    return res


print((prim(cost)))

import heapq


def prim(cost):
    V = len(cost)
    res = 0
    mincost = [float("inf")] * V
    used = [False] * V
    hq = []
    mincost[0] = 0
    heapq.heappush(hq, (0, 0))
    while hq:
        weight, u = heapq.heappop(hq)
        if used[u]:
            continue
        used[u] = True
        res += weight
        mincost[u] = weight
        for v, v_cost in cost[u]:
            if not used[v]:
                heapq.heappush(hq, (v_cost, v))
    return res


N = int(eval(input()))
xyi = []
for i in range(N):
    x, y = list(map(int, input().split()))
    xyi.append([x, y, i])
xyi.sort()
cost = [[] for _ in range(N)]
for i in range(N - 1):
    xj = xyi[i][0]
    j = xyi[i][2]
    xk = xyi[i + 1][0]
    k = xyi[i + 1][2]
    cost[j].append((k, xk - xj))
    cost[k].append((j, xk - xj))
xyi.sort(key=lambda x: x[1])
for i in range(N - 1):
    yj = xyi[i][1]
    j = xyi[i][2]
    yk = xyi[i + 1][1]
    k = xyi[i + 1][2]
    cost[j].append((k, yk - yj))
    cost[k].append((j, yk - yj))
print((prim(cost)))

-N = int(eval(input())) -z = [tuple(map(int, input().split())) for i in range(N)] -z.sort() -y = [(z[i][1], i) for i in range(N)] -y.sort() -cost = [[] for i in range(N)] -for i in range(N - 1): -    j = y[i][1] -    cost[i].append((i - 1, min(abs(z[i][0] - z[i - 1][0]), abs(z[i][1] - z[i - 1][1])))) -    j_ = y[i - 1][1] -    cost[j].append((j_, min(abs(y[i][0] - y[i - 1][0]), abs(z[j][0] - z[j_][0])))) -    cost[i].append((i + 1, min(abs(z[i + 1][0] - z[i][0]), abs(z[i + 1][1] - z[i][1])))) -    j_ = y[i + 1][1] -    cost[j].append((j_, min(abs(y[i + 1][0] - y[i][0]), abs(z[j_][1] - z[j][1])))) -i = N - 1 -j = y[i][1] -cost[i].append((i - 1, min(abs(z[i][0] - z[i - 1][0]), abs(z[i][1] - z[i - 1][1])))) -j_ = y[i - 1][1] -cost[j].append((j_, min(abs(y[i][0] - y[i - 1][0]), abs(z[j][0] - z[j_][0])))) -        if not used[u]: -            used[u] = True -            res += weight -            mincost[u] = weight -            for v, v_cost in cost[u]: -                if not used[v]: -                    heapq.heappush(hq, (v_cost, v)) +        if used[u]: +            continue +        used[u] = True +        res += weight +        mincost[u] = weight +        for v, v_cost in cost[u]: +            if not used[v]: +                heapq.heappush(hq, (v_cost, v)) +N = int(eval(input())) +xyi = [] +for i in range(N): +    x, y = list(map(int, input().split())) +    xyi.append([x, y, i]) +xyi.sort() +cost = [[] for _ in range(N)] +for i in range(N - 1): +    xj = xyi[i][0] +    j = xyi[i][2] +    xk = xyi[i + 1][0] +    k = xyi[i + 1][2] +    cost[j].append((k, xk - xj)) +    cost[k].append((j, xk - xj)) +xyi.sort(key=lambda x: x[1]) +for i in range(N - 1): +    yj = xyi[i][1] +    j = xyi[i][2] +    yk = xyi[i + 1][1] +    k = xyi[i + 1][2] +    cost[j].append((k, yk - yj)) +    cost[k].append((j, yk - yj))