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: Runtime Error)

#include <bits/stdc++.h>
const int INF = 1e9;
const int MODk = 1e9+7;
using LL = long long;
const LL LINF = 1e18;
const double EPS = 1e-10;
using namespace std;
#define COUT(v) cout<<(v)<<endl
#define COUTDOUBLE(v) cout << std::setprecision(10)<<(v)<<endl;
#define CIN(n)  int(n);cin >> (n)
#define LCIN(n) LL(n);cin >> (n)
#define SCIN(n) string(n);cin >> (n)
#define YES(n) cout<<((n)? "YES" : "NO")<<endl
#define Yes(n) cout<<((n)? "Yes" : "No")<<endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) <<endl

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)

#define FOREACH(x,a) for(auto& (x) : (a) )

#define ALL(obj) (obj).begin(),(obj).end()

#define P pair<LL,LL>
#define I vector<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
#define rmsame(a) sort(ALL(a)),a.erase(unique(ALL(a)), a.end())
#define ENDL cout<<endl
#define f first
#define s second

typedef string::const_iterator State;
class PalseError {};
class Edge{
public:
    int from,to,value;
    Edge(LL a,LL b,LL c){
        from = a;
        to = b;
        value = c;
    }
    Edge(LL a,LL b){
        from = a;
        to = b;
    }
};


class UnionFind{
private:
    vector<int> vec;
public:
    void init(int N){
        vec.resize(N);
        for(int a = 0;a < N;a++)
        {
            vec[a]=-1;
        }
    }
    int root(int a)
    {
        if(vec[a] < 0)return a;
        else return vec[a] = root(vec[a]);
    }
    bool connect(int a,int b)
    {
        a = root(a);
        b = root(b);
        if(a==b)return false;
        if(vec[a] > vec[b])swap(a,b);
        vec[a] += vec[b];
        vec[b] = a;
        return true;
    }
    bool check(int a,int b){
        a = root(a);
        b = root(b);
        if(a==b)return true;
        else return false;
    }
};
bool comp(const Edge& e1, const Edge& e2){
    return e1.value < e2.value;
}
LL kruskal(V<Edge> Edges,int N){
    sort(ALL(Edges), comp);
    UnionFind Uni;
    Uni.init(N+1);
    LL ans = 0LL;
    for(int a = 0;a < Edges.size();a++){
        Edge e = Edges.at(a);
        if(!Uni.check(e.from,e.to)){
            Uni.connect(e.from,e.to);
            ans += e.value;
        }
    }
    return ans;
}


int main(){
    CIN(N);
    V<Edge> Edges;
    V<P> mati;
    REP(a,N){
        CIN(b);CIN(c);
        mati.push_back(P(b,c));
    }
    REP(a,N-1){
        for(int b = a + 1;b < N;b++){
            Edge e(a,b,min(abs(mati.at(a).f-mati.at(b).f),abs(mati.at(a).s-mati.at(b).s)));
            Edge w(b,a,min(abs(mati.at(a).f-mati.at(b).f),abs(mati.at(a).s-mati.at(b).s)));
            Edges.push_back(e);
            Edges.push_back(w);
        }
    }
    COUT(kruskal(Edges,N));
    return 0;
}