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)

from collections import defaultdict
from heapq import 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)]
    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()