from heapq import heappop, heappush



def prim(adjacent:list, root=0):

    n = len(adjacent)

    visited = [False] * n

    min_cost = 0

    q = [(0, root)]



    count = 0

    while q and count < n:

        weight, vi = heappop(q)

        if visited[vi]:

            continue

        visited[vi] = True

        min_cost += weight

        count += 1

        for vj, weight in adjacent[vi]:

            heappush(q, (weight, vj))

    return min_cost



n = int(eval(input()))

xordered = []

yordered = []

for i in range(n):

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

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

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

xordered.sort()

yordered.sort()



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

for i in range(n-1):

    x0, y0, v0 = xordered[i]

    x1, y1, v1 = xordered[i+1]

    weight = min(abs(x0-x1), abs(y0-y1))

    adjacent[v0].append((v1, weight))

    adjacent[v1].append((v0, weight))

    x0, y0, v0 = yordered[i]

    x1, y1, v1 = yordered[i+1]

    weight = min(abs(x0-x1), abs(y0-y1))

    adjacent[v0].append((v1, weight))

    adjacent[v1].append((v0, weight))



print((prim(adjacent)))


from heapq import heappop, heappush



def prim(adjacent:list, root=0):

    '''

    Prim法。最小全域木のコストを求める。負の閉路も可

    --------

        adjacent : 隣接リスト

        root     : 探索開始頂点

    --------

        min_cost : 最小コスト

    '''

    n = len(adjacent)

    visited = [False] * n

    min_cost = 0

    q = [(0, root)]

    while q:

        weight, vi = heappop(q)

        if visited[vi]:

            continue

        visited[vi] = True

        min_cost += weight

        for vj, weight in adjacent[vi]:

            if not visited[vj]:

                heappush(q, (weight, vj))

    return min_cost



n = int(eval(input()))

xordered = []

yordered = []

for i in range(n):

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

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

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

xordered.sort()

yordered.sort()



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

for i in range(n-1):

    x0, y0, v0 = xordered[i]

    x1, y1, v1 = xordered[i+1]

    weight = min(abs(x0-x1), abs(y0-y1))

    adjacent[v0].append((v1, weight))

    adjacent[v1].append((v0, weight))

    x0, y0, v0 = yordered[i]

    x1, y1, v1 = yordered[i+1]

    weight = min(abs(x0-x1), abs(y0-y1))

    adjacent[v0].append((v1, weight))

    adjacent[v1].append((v0, weight))



print((prim(adjacent)))



from heapq import heappop, heappush


def prim(adjacent: list, root=0):
    n = len(adjacent)
    visited = [False] * n
    min_cost = 0
    q = [(0, root)]
    count = 0
    while q and count < n:
        weight, vi = heappop(q)
        if visited[vi]:
            continue
        visited[vi] = True
        min_cost += weight
        count += 1
        for vj, weight in adjacent[vi]:
            heappush(q, (weight, vj))
    return min_cost


n = int(eval(input()))
xordered = []
yordered = []
for i in range(n):
    x, y = list(map(int, input().split()))
    xordered.append((x, y, i))
    yordered.append((y, x, i))
xordered.sort()
yordered.sort()
adjacent = [[] for _ in range(n)]
for i in range(n - 1):
    x0, y0, v0 = xordered[i]
    x1, y1, v1 = xordered[i + 1]
    weight = min(abs(x0 - x1), abs(y0 - y1))
    adjacent[v0].append((v1, weight))
    adjacent[v1].append((v0, weight))
    x0, y0, v0 = yordered[i]
    x1, y1, v1 = yordered[i + 1]
    weight = min(abs(x0 - x1), abs(y0 - y1))
    adjacent[v0].append((v1, weight))
    adjacent[v1].append((v0, weight))
print((prim(adjacent)))

from heapq import heappop, heappush


def prim(adjacent: list, root=0):
    """
    Prim法。最小全域木のコストを求める。負の閉路も可
    --------
        adjacent : 隣接リスト
        root     : 探索開始頂点
    --------
        min_cost : 最小コスト
    """
    n = len(adjacent)
    visited = [False] * n
    min_cost = 0
    q = [(0, root)]
    while q:
        weight, vi = heappop(q)
        if visited[vi]:
            continue
        visited[vi] = True
        min_cost += weight
        for vj, weight in adjacent[vi]:
            if not visited[vj]:
                heappush(q, (weight, vj))
    return min_cost


n = int(eval(input()))
xordered = []
yordered = []
for i in range(n):
    x, y = list(map(int, input().split()))
    xordered.append((x, y, i))
    yordered.append((y, x, i))
xordered.sort()
yordered.sort()
adjacent = [[] for _ in range(n)]
for i in range(n - 1):
    x0, y0, v0 = xordered[i]
    x1, y1, v1 = xordered[i + 1]
    weight = min(abs(x0 - x1), abs(y0 - y1))
    adjacent[v0].append((v1, weight))
    adjacent[v1].append((v0, weight))
    x0, y0, v0 = yordered[i]
    x1, y1, v1 = yordered[i + 1]
    weight = min(abs(x0 - x1), abs(y0 - y1))
    adjacent[v0].append((v1, weight))
    adjacent[v1].append((v0, weight))
print((prim(adjacent)))

+    """ +    Prim法。最小全域木のコストを求める。負の閉路も可 +        adjacent : 隣接リスト +        root     : 探索開始頂点 +        min_cost : 最小コスト +    """ -    count = 0 -    while q and count < n: +    while q: -        count += 1 -            heappush(q, (weight, vj)) +            if not visited[vj]: +                heappush(q, (weight, vj))