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: Accepted)

//#include    <bits/stdc++.h>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <cassert>
#include <iterator>
#include <complex>
#define N  100005
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
template<typename T> inline T max(T a,T b,T c){
    return max(a,max(b,c));
}
template<typename T> inline T min(T a,T b,T c){
    return min(a,min(b,c));
}
template<typename T> inline T max(T a,T b,T c,T d){
    return max(a,max(b,c,d));
}
template<typename T> inline T min(T a,T b,T c,T d){
    return min(a,min(b,c,d));
}
const int  dx[]={0,1,0,-1,0,1,-1,1,-1};
const int  dy[]={0,0,1,0,-1,1,-1,-1,1};
typedef long long ll;
using namespace std;
//coding...........................
int parent[N];
struct coordinate
{
    int x;
    int y;
    int id;
}a[N];
struct point
{
    int start;
    int ending;
    int cost;
}b[N*4];

int cmp1(coordinate n,coordinate m)
{
    return n.x<m.x;
}
int cmp2(coordinate n,coordinate m)
{
    return n.y<m.y;
}
int cmp3(point n,point m)
{
    return n.cost<m.cost;
}
int Find(int x)
{
    if(parent[x]==x)
      return x;
    else
      return parent[x]=Find(parent[x]);

}
int main()
{
    int n;
    cin>>n;
    for (int i=1;i<=n;i++)
       {
           cin>>a[i].x>>a[i].y;
           a[i].id=i;
       }
     sort(a+1,a+1+n,cmp1);
     int k=0;
     for (int i=1;i<n;i++)
     {
         b[k].start=a[i].id;
         b[k].ending=a[i+1].id;
         b[k].cost=abs(a[i+1].x-a[i].x);
         k++;
     }
     sort(a+1,a+1+n,cmp2);
     for (int i=1;i<n;i++)
     {
         b[k].start=a[i].id;
         b[k].ending=a[i+1].id;
         b[k].cost=abs(a[i+1].y-a[i].y);
         k++;
     }
     sort(b,b+k,cmp3);
     for (int i=1;i<=n;i++)
        parent[i]=i;
     ll ans=0;
     for (int i=0;i<k;i++)
     {
         int w=Find(b[i].start);
         int v=Find(b[i].ending);
         if (w!=v)
         {
             ans+=b[i].cost;
             parent[w]=v;
         }
     }
     cout << ans << endl;
    return 0;
}