import sys

input = sys.stdin.readline



from heapq import heappop,heappush



n = int(eval(input()))

p = []

for i in range(n):

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

    p.append((i,x,y))



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



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

n0,x0,y0 = p[0]

for i in range(n-1):

    n1,x1,y1 = p[i+1]

    e[n0].append([min(abs(x1-x0),abs(y1-y0)),n1])

    e[n1].append([min(abs(x1-x0),abs(y1-y0)),n0])

    n0,x0,y0 = n1,x1,y1



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

n0,x0,y0 = p[0]

for i in range(n-1):

    n1,x1,y1 = p[i+1]

    e[n0].append([min(abs(x1-x0),abs(y1-y0)),n1])

    e[n1].append([min(abs(x1-x0),abs(y1-y0)),n0])

    n0,x0,y0 = n1,x1,y1



def Prim(n,edge):

    #edge[source] = [[cost_1,target_1],[cost_2,target_2],...]

    used = [False] * n

    q = []

    #e = [cost,target]

    for e in edge[0]:

        heappush(q,e)

    used[0] = True

    res = 0

    while q:

        c,v = heappop(q)

        if used[v]:

            continue

        used[v] = True

        for e in edge[v]:

            if not used[e[1]]:

                heappush(q,e)

        res += c

    return res



print((Prim(n,e)))
from heapq import heappop,heappush



n = int(eval(input()))

p = []

for i in range(n):

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

    p.append((i,x,y))



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



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

n0,x0,y0 = p[0]

for i in range(n-1):

    n1,x1,y1 = p[i+1]

    e[n0].append((min(abs(x1-x0),abs(y1-y0)),n1))

    e[n1].append((min(abs(x1-x0),abs(y1-y0)),n0))

    n0,x0,y0 = n1,x1,y1



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

n0,x0,y0 = p[0]

for i in range(n-1):

    n1,x1,y1 = p[i+1]

    e[n0].append((min(abs(x1-x0),abs(y1-y0)),n1))

    e[n1].append((min(abs(x1-x0),abs(y1-y0)),n0))

    n0,x0,y0 = n1,x1,y1



def Prim(n,edge):

    #edge[source] = [(cost_1,target_1),(cost_2,target_2),...]

    used = [False] * n

    q = []

    #e = [cost,target]

    for e in edge[0]:

        heappush(q,e)

    used[0] = True

    res = 0

    while q:

        c,v = heappop(q)

        if used[v]:

            continue

        used[v] = True

        for e in edge[v]:

            if not used[e[1]]:

                heappush(q,e)

        res += c

    return res



print((Prim(n,e)))
import sys

input = sys.stdin.readline
from heapq import heappop, heappush

n = int(eval(input()))
p = []
for i in range(n):
    x, y = list(map(int, input().split()))
    p.append((i, x, y))
e = [[] for _ in range(n)]
p = sorted(p, key=lambda x: x[1])
n0, x0, y0 = p[0]
for i in range(n - 1):
    n1, x1, y1 = p[i + 1]
    e[n0].append([min(abs(x1 - x0), abs(y1 - y0)), n1])
    e[n1].append([min(abs(x1 - x0), abs(y1 - y0)), n0])
    n0, x0, y0 = n1, x1, y1
p = sorted(p, key=lambda x: x[2])
n0, x0, y0 = p[0]
for i in range(n - 1):
    n1, x1, y1 = p[i + 1]
    e[n0].append([min(abs(x1 - x0), abs(y1 - y0)), n1])
    e[n1].append([min(abs(x1 - x0), abs(y1 - y0)), n0])
    n0, x0, y0 = n1, x1, y1


def Prim(n, edge):
    # edge[source] = [[cost_1,target_1],[cost_2,target_2],...]
    used = [False] * n
    q = []
    # e = [cost,target]
    for e in edge[0]:
        heappush(q, e)
    used[0] = True
    res = 0
    while q:
        c, v = heappop(q)
        if used[v]:
            continue
        used[v] = True
        for e in edge[v]:
            if not used[e[1]]:
                heappush(q, e)
        res += c
    return res


print((Prim(n, e)))

from heapq import heappop, heappush

n = int(eval(input()))
p = []
for i in range(n):
    x, y = list(map(int, input().split()))
    p.append((i, x, y))
e = [[] for _ in range(n)]
p = sorted(p, key=lambda x: x[1])
n0, x0, y0 = p[0]
for i in range(n - 1):
    n1, x1, y1 = p[i + 1]
    e[n0].append((min(abs(x1 - x0), abs(y1 - y0)), n1))
    e[n1].append((min(abs(x1 - x0), abs(y1 - y0)), n0))
    n0, x0, y0 = n1, x1, y1
p = sorted(p, key=lambda x: x[2])
n0, x0, y0 = p[0]
for i in range(n - 1):
    n1, x1, y1 = p[i + 1]
    e[n0].append((min(abs(x1 - x0), abs(y1 - y0)), n1))
    e[n1].append((min(abs(x1 - x0), abs(y1 - y0)), n0))
    n0, x0, y0 = n1, x1, y1


def Prim(n, edge):
    # edge[source] = [(cost_1,target_1),(cost_2,target_2),...]
    used = [False] * n
    q = []
    # e = [cost,target]
    for e in edge[0]:
        heappush(q, e)
    used[0] = True
    res = 0
    while q:
        c, v = heappop(q)
        if used[v]:
            continue
        used[v] = True
        for e in edge[v]:
            if not used[e[1]]:
                heappush(q, e)
        res += c
    return res


print((Prim(n, e)))

-import sys - -input = sys.stdin.readline -    e[n0].append([min(abs(x1 - x0), abs(y1 - y0)), n1]) -    e[n1].append([min(abs(x1 - x0), abs(y1 - y0)), n0]) +    e[n0].append((min(abs(x1 - x0), abs(y1 - y0)), n1)) +    e[n1].append((min(abs(x1 - x0), abs(y1 - y0)), n0)) -    e[n0].append([min(abs(x1 - x0), abs(y1 - y0)), n1]) -    e[n1].append([min(abs(x1 - x0), abs(y1 - y0)), n0]) +    e[n0].append((min(abs(x1 - x0), abs(y1 - y0)), n1)) +    e[n1].append((min(abs(x1 - x0), abs(y1 - y0)), n0)) -    # edge[source] = [[cost_1,target_1],[cost_2,target_2],...] +    # edge[source] = [(cost_1,target_1),(cost_2,target_2),...]