### BUGGY CODE ###

from collections import defaultdict
from heapq import heapify, heappush, heappop


class PrimNode:

    def __init__(self, index, cost):
        self.index = index
        self.cost = cost

    def __lt__(self, other):
        return self.cost < other.cost


def prim(N, adj_nodes, costs, initial=0):
    frontier = [PrimNode(initial, 0)]
    heapify(frontier)
    explored = [0 for _ in range(N)]
    mst_cost = 0
    while len(frontier) > 0:
        node = heappop(frontier)
        if explored[node.index] == 1:
            continue
        mst_cost += node.cost
        explored[node.index] = 1
        next_node_indices = adj_nodes[node.index]
        for next_node_index in next_node_indices:
            if explored[next_node_index] == 1:
                continue
            heappush(frontier, PrimNode(next_node_index, costs[(node.index, next_node_index)]))
    return mst_cost


class Point:

    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    L = 10**12  # large number
    adj_nodes = defaultdict(list)
    costs = defaultdict(lambda:L)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        adj_nodes[p.index].append(next_p.index)
        adj_nodes[next_p.index].append(p.index)
        costs[(p.index, next_p.index)] = cost
        costs[(next_p.index, p.index)] = cost
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        adj_nodes[p.index].append(next_p.index)
        adj_nodes[next_p.index].append(p.index)
        costs[(p.index, next_p.index)] = min([cost, costs[(p.index, next_p.index)]])
        costs[(next_p.index, p.index)] = min([cost, costs[(next_p.index, p.index)]])
    return adj_nodes, costs


def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    adj_nodes, costs = create_graph(N, points)
    ans = prim(N, adj_nodes, costs)
    print(ans)


if __name__ == '__main__':
    main()

### FIXED CODE ###

from collections import defaultdict
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree


class Point:
 
    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    # Caclulate costs
    costs = defaultdict(int)
    costs_defined = defaultdict(int)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        costs[(min_index, max_index)] = cost
        costs_defined[(min_index, max_index)] = 1
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        current_cost = costs[(min_index, max_index)]
        if costs_defined[(min_index, max_index)]:
            new_cost = min([cost, current_cost])
            costs[(min_index, max_index)] = new_cost
        else:
            costs[(min_index, max_index)] = cost
    # Create csgraph
    rows = list()
    cols = list()
    data = list()
    for node_pair, cost in costs.items():
        rows.append(node_pair[0])
        cols.append(node_pair[1])
        data.append(cost)
    return csr_matrix((data, (rows, cols)), shape=(N, N))

 
def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    graph = create_graph(N, points)
    mst = minimum_spanning_tree(graph)
    print(int(mst.sum()))
 
 
if __name__ == '__main__':
    main()