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 heappop, heappush
from operator import itemgetter

N = int(input())
town = [[int(x) for x in input().split()] + [i] for i in range(N)]


class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)


class Graph(object):
    """
    隣接リストによる有向グラフ
    """

    def __init__(self):
        self.graph = defaultdict(dict)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):
        self.graph[src][dst] = weight

    def get_nodes(self):
        return self.graph.keys()


class Kruskal(object):
    """
    クラスカル法による最小全域木
    O(ElogV)
    """

    def __init__(self, graph):
        self.edge = []
        self.total = 0

        N = len(graph)
        g = graph.graph
        uf = UnionFind(N)

        Q = []
        for src, v in g.items():
            for dst, cost in v.items():
                heappush(Q, (cost, (src, dst)))

        while len(self.edge) < N - 1 and Q:
            cost, edge = heappop(Q)
            if uf.find(edge[0]) != uf.find(edge[1]):
                self.total += cost
                self.edge.append(edge)
                uf.union(*edge)


def built(g, town):
    for i in range(N - 1):
        t1, t2 = town[i], town[i + 1]
        dx = abs(t1[0] - t2[0])
        dy = abs(t1[1] - t2[1])
        c = min(dx, dy)
        g.add_edge(t1[2], t2[2], c)
        g.add_edge(t2[2], t1[2], c)
    return g


g = Graph()
g = built(g, sorted(town, key=itemgetter(0)))
g = built(g, sorted(town, key=itemgetter(1)))
print(Kruskal(g).total)