#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define int long long
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector< pair < int , pair < ii , ii > > > viiiii;
map <ii,int> r;
map<ii,ii> p;
viiiii edge;
int numset;
ii a[100005];
void init(int N)
{
	numset=N;
	for(int i=0 ; i<N ; i++)
	{
		p[a[i]] = a[i];
	}
}
ii find(ii i)
{
	if(p[i].fi == i.fi && p[i].se == i.se)
	return p[i];
	else return find(p[i]); 
}
bool same(ii i, ii j)
{
	ii u = find(i);
	ii v = find(j);
	if(u .fi == v .fi && u .se == v .se) return 1;
	return 0;
}
void joint(ii i, ii j)
{
	if(same(i,j)==0)
	{
		numset--;
		r[find(j)]+=r[find(i)];
		p[find(i)]=find(j);
	}
}
int size(ii i)
{
	return r[find(i)];
}
int kruskal(int V)
{
	sort(edge.begin(),edge.end());
	init(V);
	int cost = 0;
	for(int i=0 ; i < edge.size(); i++)
	{
		pair < int , pair < ii , ii > > front = edge[i];
		if(same(front.se.fi,front.se.se) == 0)
		{	
			cost += front.fi;
			joint(front.se.fi,front.se.se);
		}
	}
	return cost;
}

bool cmp(ii a , ii b)
{
	return a . se < b .se;
}
signed main(){
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; ++i)
	{
		int x, y;
		cin >> x >> y;
		r[a[i]] = 1;
		a[i].fi = x;
		a[i].se = y;
	}
	sort(a + 1, a + n + 1);
	for(int i = 1 ; i < n ; ++i)
	{
		for(int j = i + 1 ; j <= i + 50 && j <= n ; ++j)
		{
			edge.pb(mp(a[j].fi - a[i].fi,mp(a[i],a[j])));
		}
	}
	sort(a + 1, a + n + 1, cmp);
	for(int i = 1 ; i < n ; ++i)
	{
		for(int j = i + 1 ;j <= i + 50 && j <= n; ++j)
		{
			edge.pb(mp(a[j].se - a[i].se,mp(a[i],a[j])));
		}
	}
	co