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 9.2.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int stw[200000][3];
int stw_s[200000][3];
int index_stw[200000];
int par[100000];
int x[100000], y[100000];

int root(int x){
    if(par[x] == x){return x;}
    return par[x] = root(par[x]);
}

int unite(int x, int y){
    int rx = root(x);
    int ry = root(y);
    if(rx == ry){
        return 0;
    }
    par[rx] = ry;
    return 1;
}

int compare_int_x(const void *a, const void *b)
{
    return x[*(int*)a] - x[*(int*)(b)];
}
int compare_int_y(const void *a, const void *b)
{
    return y[*(int*)a] - y[*(int*)(b)];
}
int compare_int_stw(const void *a, const void *b)
{
    return stw[*(int*)a][2] - stw[*(int*)(b)][2];
}

int same(int x, int y){
    int rx = root(x);
    int ry = root(y);
    if(rx == ry){
        return 1;
    }
    return 0;
}
int index_x[100000];
int index_y[100000];
int main(void){
    int n;
    int i, j, k;
    
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        scanf("%d%d", &x[i], &y[i]);
        index_x[i] = i;
        index_y[i] = i;
    }

    qsort(index_x, n, sizeof(int), compare_int_x);
    qsort(index_y, n, sizeof(int), compare_int_y);




    int cnt = 0;
    for(i = 1; i < n; i++){
        stw[cnt][0] = index_x[i];
        stw[cnt][1] = index_x[i-1];
        stw[cnt][2] = abs(x[index_x[i]] - x[index_x[i-1]]);
        cnt++;
    }
    for(i = 1; i < n; i++){
        stw[cnt][0] = index_y[i];
        stw[cnt][1] = index_y[i-1];
        stw[cnt][2] = abs(y[index_y[i]] - y[index_y[i-1]]);
        cnt++;
    }

    for(i = 0; i < cnt; i++){
        index_stw[i] = i;
    }

    qsort(index_stw, cnt, sizeof(int), compare_int_stw);

    for(i = 0; i < cnt; i++){
        stw_s[i][0] = stw[index_stw[i]][0];
        stw_s[i][1] = stw[index_stw[i]][1];
        stw_s[i][2] = stw[index_stw[i]][2];
        
    }


    //クラスカル法
    for(i = 0; i < n; i++){
        par[i] = i;
    }

    long long int ans = 0;
    for(i = 0; i < cnt; i++){
        //stw_s[i][]の辺を取り出す
        if(same(stw_s[i][0], stw_s[i][1]) == 0){
            unite(stw_s[i][0], stw_s[i][1]);
            ans += stw_s[i][2];
            
        }
    }

    printf("%lld\n", ans);
    return 0;






}