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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
int par[1000000],rank[1000000];
void init(int n){
    int i;
    for(i=0;i<n;i++){
        par[i]=1;
        rank[i]=0;
    }
}
int root(int x){
    if(par[x]==x) return x;
    return root(par[x]);
}
void combsort(int a[],int b[],int c[],int n){
    int i,h=n/1.3,swaped,tmp;
    if(h==0) h=1;
    while(h!=0||swaped!=0){
        swaped=0;
        if(h==0) h=1;
        if(h==10||h==9) h=11;
        for(i=0;i<n-h;i++){
            if(a[i]>a[i+h]){
                tmp=a[i];
                a[i]=a[i+h];
                a[i+h]=tmp;
                if(b!=NULL){
                    tmp=b[i];
                    b[i]=b[i+h];
                    b[i+h]=tmp;
                }
                if(c!=NULL){
                    tmp=c[i];
                    c[i]=c[i+h];
                    c[i+h]=c[i];
                }
                swaped=1;
            }
        }
        h=h/1.3;
    }
}
void unite(int x,int y){
    x=root(x);
    y=root(y);
    if(x!=y){
        int rx=rank[x],ry=rank[y];
        if(rx<ry){
            par[x]=y;
        }else{
            par[y]=x;
        }
        if(rx==ry) rank[x]++;
    }
}
int same(int x,int y){
    if(root(x)==root(y)) return 1;
    return 0;
}
int main(void){
    int n,i,cnt=0,sum=0;
    int x[1000000],y[1000000],n1[1000000],n2[1000000];
    int from[2000000],to[2000000],cost[2000000]={0};
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d%d",&x[i],&y[i]);
        n1[i]=i;
        n2[i]=i;
    }
    combsort(x,n1,NULL,n);
    combsort(y,n2,NULL,n);
    for(i=1;i<n;i++){
        from[cnt]=n1[i-1];
        to[cnt]=n1[i];
        cost[cnt]=x[i]-x[i-1];
        cnt++;
        from[cnt]=n2[i-1];
        to[cnt]=n2[i];
        cost[cnt]=y[i]-y[i-1];
        cnt++;
    }
    combsort(cost,from,to,cnt);
    init(cnt);
    for(i=0;i<cnt;i++){
        if(same(from[i],to[i])==0){
            unite(from[i],to[i]);
            sum+=cost[i];
        }
    }
    printf("%d\n",n2[0]);
    return 0;
}