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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
const int inf=1e9+7;
const ll mod=1e9+7;
const ll big=1e18;

int main() {
    int N;
    cin>>N;
    vector<tuple<ll, ll, int> > arr1, arr2;
    ll x, y;
    for(int i=0;i<N;++i){
        cin>>x>>y;
        arr1.push_back(make_tuple(x, y, i));
        arr2.push_back(make_tuple(y, x, i));
    }
    sort(arr1.begin(), arr1.end());
    sort(arr2.begin(), arr2.end());
    vector<vector<pair<int, ll> > > edge(N);
    int town1, town2;
    ll x1, y1, x2, y2;
    for(int i=1;i<N;++i){
        town1 = get<2>(arr1[i]);
        town2 = get<2>(arr1[i-1]);
        x1 = get<0>(arr1[i]);
        x2 = get<0>(arr1[i-1]);
        edge[town1].push_back(make_pair(town2, x1-x2));
        edge[town2].push_back(make_pair(town1, x1-x2));
        
        town1 = get<2>(arr2[i]);
        town2 = get<2>(arr2[i-1]);
        y1 = get<0>(arr2[i]);
        y2 = get<0>(arr2[i-1]);
        edge[town1].push_back(make_pair(town2, y1-y2));
        edge[town2].push_back(make_pair(town1, y1-y2));
    }

    priority_queue<pair<ll, int>, vector<pair<ll, int> >, greater<pair<ll, int> > > que;
    que.push(make_pair(0, 0));

    ll DP[N];
    for(int i=0;i<N;++i){
        DP[i] = -1;
    }

    pair<ll, int> state;
    int town;
    ll cost;
    while(!que.empty()){
        state = que.top();que.pop();
        town = state.se;
        cost = state.fi;
        if(DP[town] != -1) continue;
        DP[town] = cost;
        for(int i=0;i<edge[town].size();++i){
            if(DP[edge[town][i].fi] != -1) continue;
            que.push(make_pair(edge[town][i].se, edge[town][i].fi));
        }
    }
    ll ans = 0;
    for(int i=0;i<N;++i){
        ans += DP[i];
    }
    cout<<ans<<endl;
}