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 PyPy3 (2.4.0) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define int long long
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

int N;
vector<P> xi, yi;
vector<T> edges;

struct Unionfind {
    vector<int> par, rank;
    
    Unionfind(int n) {
        par.resize(n);
        fill(par.begin(), par.end(), -1);
        rank.resize(n);
    }
    
    int root(int x) {
        int r = x;
        while (!(par[r]<0)) r = par[r];
        int t = x;
        
        while (t!=r) {
            int tmp = t;
            t = par[t];
            par[tmp] = r;
        }
        
        return r;
    }
    
    void unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        
        if (rx==ry) return;
        
        if (rank[rx]<=rank[ry]) {
            par[ry] += par[rx];
            par[rx] = ry;
            
            if (rank[rx]==rank[ry]) rank[ry]++;
        }
        else {
            par[rx] += par[ry];
            par[ry] = rx;
        }
    }
    
    bool is_same(int x, int y) {
        return root(x)==root(y);
    }
    
    int count(int x) {
        return -par[root(x)];
    }
};
 
signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N;
    rep(i, N) {
        int x, y; cin >> x >> y;
        xi.pb(P(x, i));
        yi.pb(P(y, i));
    }
    sort(xi.begin(), xi.end());
    sort(yi.begin(), yi.end());
    
    rep(i, N-1) {
        int s = xi[i].second;
        int t = xi[i+1].second;
        int cost = xi[i+1].first-xi[i].first;
        edges.pb(make_tuple(cost, s, t));
    }
    rep(i, N-1) {
        int s = yi[i].second;
        int t = yi[i+1].second;
        int cost = yi[i+1].first-yi[i].first;
        edges.pb(make_tuple(cost, s, t));
    }
    sort(edges.begin(), edges.end());
    
    Unionfind uf(N);
    int ans = 0;
    rep(i, 2*(N-1)) {
        int s = get<1>(edges[i]);
        int t = get<2>(edges[i]);
        int cost = get<0>(edges[i]);
        if (!uf.is_same(s, t)) {
            uf.unite(s, t);
            ans += cost;
        }
    }
    cout << ans << endl;
}