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: Time Limit Exceeded)

#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include <numeric>


#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

typedef long long int ll;
#define ARRAY_MAX 100005

vector<int> par(ARRAY_MAX);//親
vector<int> ran(ARRAY_MAX);//木の深さ

typedef struct edge{

    int u;
    int v;
    int cost;
}EDGE;

bool comp(const EDGE& e1,const EDGE& e2){
    return e1.cost < e2.cost;
}


//n要素で初期化
void init(int n){
    for(int i = 0;i < n;i++){
        par[i] = i;/*各ノードに番号を振る,この時par[x]=xとなる時は木の根となる*/
        ran[i] = 0;/*各ノード自体の高さは0*/
    }
}
//木の根を求める
int find(int x){
    if(par[x] == x){
        return x;/*根ならそのノードの番号を返す*/
    }else{
        return par[x] = find(par[x]);/*根でないならさらにノードの根を探す*/
    }
}

//xとyの属する集合を併合する
void unite(int x,int y){
    x = find(x);//xの根の番号を探す
    y = find(y);//yの根の番号を探す
    if(x == y){//一致すればつながっている
        return;
    }
    if(ran[x] < ran[y]){//xの高さが低いなら高いほうにつなぐ、そして高さは大きい方(y)になる
        par[x] = y;
    }else{
        par[y] = x;//yの高さが低いなら高いほうにつなぐ、そして高さは大きいほう(x)になる
        if(ran[x] == ran[y]){//高さが一致しているなら併合の高さは1増える
            ran[x]++;
        }
    }
}

//xとyが同じ集合に属するか判定
bool same(int x,int y){
    return find(x) == find(y);//同じ集合なら1、違うなら0が返る
}

int kruskal(int n,int count,vector<EDGE>& es){

    sort(es.begin(),es.end(),comp);
    init(n);
    int res = 0;
    for(int i = 0;i < count;i++){
        EDGE e = es[i];
        if(!same(e.u,e.v)){
            unite(e.u,e.v);
            res += e.cost;
        }
    }
    return res;
}



int main(){

    int n;
    cin >> n;
    vector<EDGE> es;
    vector<int> x(n),y(n);
    int count = 0;

    REP(i,n){
        cin >> x[i] >> y[i];
    }

    for(int i = 0;i < n-1;i++){
        for(int j = i+1;j < n;j++){
            EDGE e;
            e.u = i;
            e.v = j;
            e.cost = min(abs(x[j]-x[i]),abs(y[j]-y[i]));
            es.push_back(e);
            count++;
        }
    }
    int ans = kruskal(n,count,es);
    cout << ans << endl;



    return 0;
}