# coding: utf-8
import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}


def read():
    N = II()
    x, y = [], []
    for __ in range(N):
        _x, _y = ILI()
        x.append(_x)
        y.append(_y)
    return N, x, y


def solve(N, x, y):
    cost = [[INF] * N for __ in range(N)]
    for i in range(N):
        for j in range(N):
            x_i, y_i = x[i], y[i]
            x_j, y_j = x[j], y[j]
            cost[i][j] = min(abs(x_i - x_j), abs(y_i - y_j))

    min_cost = [INF] * N
    bool_used = [False] * N

    ans = 0
    min_cost[0] = 0
    while True:
        v = -1
        for i in range(N):
            if bool_used[i] is False and (v == -1 or min_cost[i] < min_cost[v]):
                v = i

        if v == -1:
            break
        bool_used[v] = True
        ans += min_cost[v]
        for i in range(N):
            min_cost[i] = min(min_cost[i], cost[v][i])

    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()
