from heapq import heapify, heappush, heappop

n = int(input())
cities = [tuple(map(int, input().split())) for _ in range(n)]
visited = {0}
x0, y0 = cities[0]
queue = [(min(abs(xi - x0), abs(yi - y0)), i) for i, (xi, yi) in enumerate(cities) if i not in visited]
heapify(queue)
total = 0
while True:
    cost, i = heappop(queue)
    if i in visited:
        continue
    visited.add(i)
    total += cost
    if len(visited) == n:
        break
    xi, yi = cities[i]
    for j, (xj, yj) in enumerate(cities):
        if j in visited:
            continue
        heappush(queue, (min(abs(xj - xi), abs(yj - yi)), j))
print(total)
