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


// Problem : D - Built?
// Contest : AtCoder - AtCoder Beginner Contest 065
// URL : https://atcoder.jp/contests/abc065/tasks/arc076_b
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int,int>
#define m_p(x,y) make_pair(x,y)
#define vp vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v,n,x) v.assign(n,x)
#define incd(v,n) v.resize(n)
#define iniz(n) memset(n,0,sizeof(n))
#define inin(n) memset(n,-1,sizeof(n))
#define inimi(n) memset(n,0xc0,sizeof(n))
#define inima(n) memset(n,0x3f,sizeof(n))
#define all(v) (v).begin(),(v).end()

using namespace std;

template<typename T1,typename T2>istream &operator>>(istream &is, vector<pair<T1,T2>> &v) { for (pair<T1,T2> &t : v) is >> t.f>>t.se; return is; }
template<typename T>istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; }
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) {os << t <<" ";} os << '\n'; return os; }

const int N=200001;
double pi=acos(-1.0);
int md=1e9+7;
const int INF=1e15;
int dx1[]={0,0,-1,1};
int dy1[]={1,-1,0,0};

template<class T>
T abst(T a)
{return a<0?-a:a;}

template<class T>
T max2(T a,T b){return a>b?a:b;}
template<class T>
T min2(T a,T b){return a<b?a:b;}

vi dsu,sz;
void init_dsu(int n)
{
	incd(dsu,n+1);
	incd(sz,n+1);
	for(int i=0;i<=n;i++)
	dsu[i]=i,sz[i]=1;
}

int get_par(int x)
{
	if(dsu[x]==x) return x;
	return dsu[x]=get_par(dsu[x]);
}

void unite(int x,int y)
{
	int p1=get_par(x);
	int p2=get_par(y);
	if(p1!=p2)
	{
		sz[p1]+=sz[p2];
		dsu[p2]=p1;
	}
}

int MST(vector<pair<int,pii>> edges,int n)
{
	init_dsu(n+1);
	int cost=0;
	sort(all(edges));
	for(auto i:edges)
	{
		pii p=i.se;
		int x=get_par(p.f);
		int y=get_par(p.se);
		if(x!=y)
		{
			//cout<<x<<" "<<y<<" "<<i.f<<"\n";
			unite(x,y);
			cost+=i.f;
		}
	}
	return cost;
}

void solve()
{
int n;cin>>n;
vector<pair<int,pii>> edges;
vp v1,v2;
int point=1;
map<pii,int> mp;
for(int i=0;i<n;i++)
{
	int x,y;
	cin>>x>>y;
	if(mp.count({x,y}))continue;
	else
	{
		mp[{x,y}]=point++;
		v1.pb({x,point-1});
		v2.pb({y,point-1});
	}
}
sort(all(v1));
sort(all(v2));
for(int i=0;i<n-1;i++)
{
	edges.pb({v1[i+1].f-v1[i].f,{v1[i].se,v1[i+1].se}});
	
}
for(int i=0;i<n-1;i++)
{
	edges.pb({v2[i+1].f-v2[i].f,{v2[i].se,v2[i+1].se}});
	
}
cout<<MST(edges,point);
}

int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)
{
    solve();
}
return 0;
}