Problem:
Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8

Solution in Python (3.4.3) (judge verdict: Time Limit Exceeded)

import heapq
import collections
import heapq
def input_as_int():
    return list(map(int,input().split()))


def get_comb(n):  # n C n まで

    fac = get_fact(n)
    com  = [["error" for _ in range(n+1)] for _ in range (n + 1)]
    for i in range(n + 1):
        for j in range(i + 1):
            if  i >= j:
                com[i][j] = fac[i] // (fac[i-j] * fac[j])

    return com


def get_fact(n):
    temp = 1
    fac = [1]
    for i in range(1, n + 1):
        temp *= i
        fac.append(temp)
    return fac

# def prime_method(graph):
#     start = graph[0]
#     n = len(graph)
#     went = [False] * n  # n個の点に行ったかという話
#     edges = [temp for temp in start ]
#     heapq.heapify(edges)
#     went[0] = True
#     cost = 0
#     while len(edges) != 0:
#         minedge = heapq.heappop(edges)
#         if went[minedge[1]]:
#             continue
#         v = minedge[1]
#         gv = graph[v]
#         for temp in gv:
#             if not went[temp[1]]:  # その頂点に行ってなければ
#                 heapq.heappush(edges, temp)
#         went[v] = True
#         cost += minedge[0]
#     return cost
    # a[1] = [[2,3],[5,11]] → 点1からコスト2で頂点3に行ける辺、点1からコスト5で頂点11に行ける辺がある

def prim_heap(edge):
    used = [True] * n #True:不使用
    edgelist = []
    for e in edge[0]:
        heapq.heappush(edgelist,e)
    used[0] = False
    res = 0
    while len(edgelist) != 0:
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,e)
        res += minedge[0]
    return res


def get_graph():
    n = input()
    graph = [input_as_int() for _ in range(n)]


    # a[1] = [[2,3],[5,11]] → 点1からコスト2で頂点3に行ける辺、点1からコスト5で頂点11に行ける辺がある


n = int(input())
cities = [(a, b, i) for i, (a, b) in enumerate([tuple(map(int, input().split())) for _ in range(n)])]

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


citiesX=sorted(cities,key=lambda x:x[0])
citiesY=sorted(cities,key=lambda x:x[1])

for i in range(n-1):


    graph[citiesX[i][2]].append([abs(citiesX[i][0] - citiesX[i+1][0]),citiesX[i+1][2]])
    graph[citiesX[i+1][2]].append([abs(citiesX[i][0] - citiesX[i+1][0]),citiesX[i][2]])
    graph[citiesY[i][2]].append([abs(citiesY[i][1] - citiesY[i+1][1]),citiesY[i+1][2]])
    graph[citiesY[i+1][2]].append([abs(citiesY[i][1] - citiesY[i+1][1]),citiesY[i][2]])

print(prim_heap(graph))