stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int n,x,y,k;

long long ans=0;

const int inf = 0x3f3f3f3f;

#define ft first

#define se second

const int maxn=100007;

pair <int,int> r[maxn];

pair <int,int> c[maxn];

struct Node{

	int u,v,cst;

}ege[2*maxn+1000];

Node make_node(int a,int b,int c){

	Node node;

	node.u=a;

	node.v=b;

	node.cst=c;

	return node;

}

int f[maxn],rnk[maxn];

int fnd(int x){

	if (f[x]==x)return x;

	return f[x]=fnd(f[x]);

}

void merge(int x,int y){

	x=fnd(x);

	y=fnd(y);

	if (rnk[x]>rnk[y])f[y]=x;

	else {

		f[x]=y;

		if (rnk[x]==rnk[y])rnk[y]++;

	}

}

bool cmp(Node a,Node b){

	return a.cst<b.cst;

}

void K(){

	for (int i=1;i<=k;++i){

		if (fnd(ege[i].u)==fnd(ege[i].v))continue;

		ans+=ege[i].cst;

		merge(ege[i].u,ege[i].v);

//		printf("%lld\n",ans);

	}

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

}

int main(){

	scanf("%d",&n);

	for (int i=0;i<n;++i){

		scanf("%d%d",&x,&y);

		r[i]=make_pair(x,i);

		c[i]=make_pair(y,i);

	}

	sort(r,r+n);

	sort(c,c+n);

	for (int i=1;i<n;++i){

		ege[++k]=make_node(r[i-1].se,r[i].se,abs(r[i].ft-r[i-1].ft));

		ege[++k]=make_node(c[i-1].se,c[i].se,abs(c[i].ft-c[i-1].ft));

	}

	sort(ege+1,ege+1+k,cmp);

	memset(f,inf,sizeof f);

	for (int i=0;i<n;++i)f[i]=i,rnk[i]=1;

	K();

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3997254082902175x):
#include<iostream>

#include<cstdio>

#include<set>

#include<algorithm>

using namespace std;

const int N=1e5+5;

struct node{

    int u,v,w;

    bool operator<(const node&a)const{return w<a.w;}

}e[N*2];

int se[N];

int get(int x){

    while(x!=se[x])

        x=se[x]=se[se[x]];

    return x;

}

struct point{

    int w,id;

    bool operator<(const point&a)const{return w<a.w;}

}x[N],y[N];

int main(){

    int n;scanf("%d",&n);

    for(int i=1;i<=n;i++){

        scanf("%d%d",&x[i].w,&y[i].w);

        x[i].id=y[i].id=i;

        se[i]=i;

    }

    sort(x+1,x+n+1),sort(y+1,y+n+1);

    int ct=0;

    for(int i=1;i<n;i++) e[++ct]=(node){x[i].id,x[i+1].id,abs(x[i].w-x[i+1].w)};

    for(int i=1;i<n;i++) e[++ct]=(node){y[i].id,y[i+1].id,abs(y[i].w-y[i+1].w)};

    sort(e+1,e+ct+1);

    long long sum=0;

    int cnt=1;

    for(int i=1;i<=ct;i++){

        int fu=get(e[i].u),fv=get(e[i].v);

        if(fu==fv)continue;

        sum+=e[i].w;

        se[fu]=fv;

        if(++cnt==n) break;

    }

    cout<<sum;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>



using namespace std;

using namespace __gnu_pbds;



#define fi first

#define se second

#define mp make_pair

#define pb push_back

#define fbo find_by_order

#define ook order_of_key



typedef long long ll;

typedef pair<ll,ll> ii;

typedef vector<int> vi;

typedef long double ld;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

bool marked[100001];

vector<ii> v[100001];  //adjList



ll prim(int k){

    priority_queue<ii, vector<ii>, greater<ii> > q;

    ll u;

    long long minimumCost = 0;

    ii p;

    q.push(mp(0, k));

    while(!q.empty())

    {

        p = q.top();

        q.pop();

        k = p.second;

        if(marked[k] == true)

            continue;

        minimumCost += p.fi;

        marked[k] = true;

        for(ll i = 0;i < v[k].size();++i)

        {

            u = v[k][i].se;

            if(marked[u] == false)

                q.push(v[k][i]);

        }

    }

    return minimumCost;

}



int main() {

    std::ios::sync_with_stdio(false);

    cin.tie(NULL);

    long long n;

    cin>>n;

    vector<ii> x;

    vector<ii> y;

    for(ll i =0;i<n;i++){

        ll a,b;

        cin>>a>>b;

        x.pb(mp(a,i));

        y.pb(mp(b,i));

    }

    sort(x.begin(),x.end());

    sort(y.begin(),y.end());

    for(ll i=0;i<n-1;i++){

        v[x[i].se].pb(mp(x[i+1].fi-x[i].fi,x[i+1].se));

        v[x[i+1].se].pb(mp(x[i+1].fi-x[i].fi,x[i].se));

        v[y[i].se].pb(mp(y[i+1].fi-y[i].fi,y[i+1].se));

        v[y[i+1].se].pb(mp(y[i+1].fi-y[i].fi,y[i].se));

    }

    ll ans=prim(0);

    cout<<ans;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.503956156311845x):
#include <bits/stdc++.h>

#define int long long

using namespace std;

typedef struct{

	int x,y,id;

} ii;

typedef tuple<int,ii,ii> t;

int dist(ii a,ii b){

	return min(abs(a.x-b.x),abs(a.y-b.y));

}

vector<int> up;

int fp(int u){

	if(up[u]<0) return u;

	else return up[u] = fp(up[u]);

}

void unite(int u,int v){

	int x = fp(u);

	int y = fp(v);

	if(x!=y) up[x]=y;

}

signed main() {

	int n;

	cin>>n;

	up.resize(n,-1);

	vector<ii> p(n);

	int i=0;

	for(auto &v:p)

	{

		v.id=i++;

		cin>>v.x>>v.y;

	}

	auto cmp= [](t a,t b){

		return get<0>(a)>get<0>(b);

	};

	priority_queue<t,vector<t>,decltype (cmp)> pq(cmp);

	sort(p.begin(),p.end(),[](ii lhs,ii rhs){return lhs.x<rhs.x;});

	for(auto it = p.begin();it!=prev(p.end());it++){

		int d = dist(*it,*next(it));

		pq.push(make_tuple(d,*it,*next(it)));

	}

	sort(p.begin(),p.end(),[](ii lhs,ii rhs){return lhs.y<rhs.y;});

		for(auto it = p.begin();it!=prev(p.end());it++){

			int d = dist(*it,*next(it));

			pq.push(make_tuple(d,*it,*next(it)));

		}

	int cnt = n;

	int res = 0;

	while(!pq.empty()){

		int d;

		ii a,b;

		tie(d,a,b) = pq.top();

		pq.pop();

		if(fp(a.id)!=fp(b.id)){

			n--;

			res+=d;

			unite(fp(a.id), fp(b.id));

		}

	}

	cout<<res<<endl;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int n,x,y,k;

long long ans=0;

const int inf = 0x3f3f3f3f;

#define ft first

#define se second

const int maxn=100007;

pair <int,int> r[maxn];

pair <int,int> c[maxn];

struct Node{

	int u,v,cst;

}ege[2*maxn+1000];

Node make_node(int a,int b,int c){

	Node node;

	node.u=a;

	node.v=b;

	node.cst=c;

	return node;

}

int f[maxn],rnk[maxn];

int fnd(int x){

	if (f[x]==x)return x;

	return f[x]=fnd(f[x]);

}

void merge(int x,int y){

	x=fnd(x);

	y=fnd(y);

	if (rnk[x]>rnk[y])f[y]=x;

	else {

		f[x]=y;

		if (rnk[x]==rnk[y])rnk[y]++;

	}

}

bool cmp(Node a,Node b){

	return a.cst<b.cst;

}

void K(){

	for (int i=1;i<=k;++i){

		if (fnd(ege[i].u)==fnd(ege[i].v))continue;

		ans+=ege[i].cst;

		merge(ege[i].u,ege[i].v);

//		printf("%lld\n",ans);

	}

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

}

int main(){

	scanf("%d",&n);

	for (int i=0;i<n;++i){

		scanf("%d%d",&x,&y);

		r[i]=make_pair(x,i);

		c[i]=make_pair(y,i);

	}

	sort(r,r+n);

	sort(c,c+n);

	for (int i=1;i<n;++i){

		ege[++k]=make_node(r[i-1].se,r[i].se,abs(r[i].ft-r[i-1].ft));

		ege[++k]=make_node(c[i-1].se,c[i].se,abs(c[i].ft-c[i-1].ft));

	}

	sort(ege+1,ege+1+k,cmp);

	memset(f,inf,sizeof f);

	for (int i=0;i<n;++i)f[i]=i,rnk[i]=1;

	K();

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3766249664965506x):
#include <bits/stdc++.h>

#define int long long

using namespace std;

typedef struct{

	int x,y,id;

} ii;

typedef tuple<int,ii,ii> t;

int dist(ii a,ii b){

	return min(abs(a.x-b.x),abs(a.y-b.y));

}

vector<int> up;

int fp(int u){

	if(up[u]<0) return u;

	else return up[u] = fp(up[u]);

}

void unite(int u,int v){

	int x = fp(u);

	int y = fp(v);

	if(x!=y) up[x]=y;

}

signed main() {

	int n;

	cin>>n;

	up.resize(n,-1);

	vector<ii> p(n);

	int i=0;

	for(auto &v:p)

	{

		v.id=i++;

		cin>>v.x>>v.y;

	}

	auto cmp= [](t a,t b){

		return get<0>(a)>get<0>(b);

	};

	priority_queue<t,vector<t>,decltype (cmp)> pq(cmp);

	sort(p.begin(),p.end(),[](ii lhs,ii rhs){return lhs.x<rhs.x;});

	for(auto it = p.begin();it!=prev(p.end());it++){

		int d = dist(*it,*next(it));

		pq.push(make_tuple(d,*it,*next(it)));

	}

	sort(p.begin(),p.end(),[](ii lhs,ii rhs){return lhs.y<rhs.y;});

		for(auto it = p.begin();it!=prev(p.end());it++){

			int d = dist(*it,*next(it));

			pq.push(make_tuple(d,*it,*next(it)));

		}

	int cnt = n;

	int res = 0;

	while(!pq.empty()){

		int d;

		ii a,b;

		tie(d,a,b) = pq.top();

		pq.pop();

		if(fp(a.id)!=fp(b.id)){

			n--;

			res+=d;

			unite(fp(a.id), fp(b.id));

		}

	}

	cout<<res<<endl;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int n,x,y,k;

long long ans=0;

const int inf = 0x3f3f3f3f;

#define ft first

#define se second

const int maxn=100007;

pair <int,int> r[maxn];

pair <int,int> c[maxn];

struct Node{

	int u,v,cst;

}ege[2*maxn+1000];

Node make_node(int a,int b,int c){

	Node node;

	node.u=a;

	node.v=b;

	node.cst=c;

	return node;

}

int f[maxn],rnk[maxn];

int fnd(int x){

	if (f[x]==x)return x;

	return f[x]=fnd(f[x]);

}

void merge(int x,int y){

	x=fnd(x);

	y=fnd(y);

	if (rnk[x]>rnk[y])f[y]=x;

	else {

		f[x]=y;

		if (rnk[x]==rnk[y])rnk[y]++;

	}

}

bool cmp(Node a,Node b){

	return a.cst<b.cst;

}

void K(){

	for (int i=1;i<=k;++i){

		if (fnd(ege[i].u)==fnd(ege[i].v))continue;

		ans+=ege[i].cst;

		merge(ege[i].u,ege[i].v);

//		printf("%lld\n",ans);

	}

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

}

int main(){

	scanf("%d",&n);

	for (int i=0;i<n;++i){

		scanf("%d%d",&x,&y);

		r[i]=make_pair(x,i);

		c[i]=make_pair(y,i);

	}

	sort(r,r+n);

	sort(c,c+n);

	for (int i=1;i<n;++i){

		ege[++k]=make_node(r[i-1].se,r[i].se,abs(r[i].ft-r[i-1].ft));

		ege[++k]=make_node(c[i-1].se,c[i].se,abs(c[i].ft-c[i-1].ft));

	}

	sort(ege+1,ege+1+k,cmp);

	memset(f,inf,sizeof f);

	for (int i=0;i<n;++i)f[i]=i,rnk[i]=1;

	K();

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3627890468078976x):
#include<bits/stdc++.h>

using namespace std;



const int mx=1e6+5;



typedef long long ll;

typedef pair<ll,ll> ii;

typedef pair<ii,ll> iii;



int par[mx], rnk[mx];



void init(int n) {

	for (int i=0; i<n; i++)

		par[i]=i, rnk[i]=0;

}



int find(int x) {

	if(par[x]==x) return x;

	else return par[x]=find(par[x]);

}



void unite(int x, int y) {

	x=find(x);

	y=find(y);

	if(x==y) return;

	if(rnk[x]<rnk[y]) par[x]=y;

	else {

		par[y]=x;

		if(rnk[x]==rnk[y]) rnk[x]++;

	}

}



bool same(int x, int y) {

	return find(x)==find(y);

}



int main() {

	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

	int N; cin>>N;

	vector<iii> v(N);

	for (int i=0; i<N; i++) {

		int x,y; cin>>x>>y;

		v[i]={{x,y},i+1};

	}

	vector<iii> edges;

	sort(v.begin(),v.end(),[&](const iii &a, const iii &b) {

		return a.first.first<b.first.first;

	});

	for (int i=1; i<N; i++) {

		int x=v[i].second, y=v[i-1].second, w=v[i].first.first-v[i-1].first.first;

		edges.push_back({{x,y},w});

	}

	sort(v.begin(),v.end(),[&](const iii &a, const iii &b) {

		return a.first.second<b.first.second;

	});

	for (int i=1; i<N; i++) {

		int x=v[i].second, y=v[i-1].second, w=v[i].first.second-v[i-1].first.second;

		edges.push_back({{x,y},w});

	}

	sort(edges.begin(),edges.end(),[&](const iii &a, const iii &b) {

		return a.second<b.second;

	});

	init(N+1);

	ll ret=0;

	for (auto e : edges) {

		int x=e.first.first, y=e.first.second, w=e.second;

		if(same(x,y)) continue;

		unite(x,y);

		ret+=w;

	}

	cout<<ret<<"\n";

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<fstream>

#include<sstream>

#include<algorithm>

#include<cstdio>

#include<cassert>

#include<cmath>

#include<cstdlib>

#include<cstring>

#include<string>

#include<deque>

#include<queue>

#include<vector>

#include<list>

#include<map>

#include<set>

#include<bitset>

#include<stack>

#include<iomanip>

#include<utility>

#include<functional>

using namespace std;

int n,m;

int ans;

int par[100005];

vector<int> edge[100005];

vector<pair<int,int> > el;

pair<int,int> e[200005];

pair<pair<int,int>,int> a[100005],b[100005];

int find(int x)

{

	if(par[x]!=x)

		return par[x]=find(par[x]);

	else

		return par[x];

}

bool unite(int x,int y)

{

	x=find(x);

	y=find(y);

	if(x==y)

		return false;

	else

	{

		par[x]=y;

		return true;

	}

}

int main()

{

	cin>>n;

	for(int i=0;i<n;i++)

	{

		cin>>a[i].first.first>>a[i].first.second;

		b[i].first.first=a[i].first.second;

		b[i].first.second=a[i].first.first;

		a[i].second=b[i].second=i;

	}

	sort(a,a+n);

	sort(b,b+n);

	for(int i=1;i<n;i++)

	{

		edge[a[i].second].push_back(a[i-1].second);

		e[m]=make_pair(a[i].second,a[i-1].second);

		el.push_back(make_pair(a[i].first.first-a[i-1].first.first,m++));

	}

	for(int i=1;i<n;i++)

	{

		edge[b[i].second].push_back(b[i-1].second);

		e[m]=make_pair(b[i].second,b[i-1].second);

		el.push_back(make_pair(b[i].first.first-b[i-1].first.first,m++));

	}

	for(int i=0;i<n;i++)

		par[i]=i;

	sort(el.begin(),el.end());

	for(int i=0;i<m;i++)

	{

		int xx=e[el[i].second].first;

		int yy=e[el[i].second].second;

		if(unite(xx,yy))

			ans+=el[i].first;

	}

	cout<<ans;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.529795928812361x):
#include<bits/stdc++.h>

using namespace std;

int f[100005];

int p;

long long cost;



struct edge{

	int x,y,w;

	bool operator < (edge e){

		return w<e.w;

	}

}e[200005];



struct node{

	int id,x,y;

}a[100005];

int N;



bool cmp1(node x,node y){

	return x.x<y.x;

}



bool cmp2(node x,node y){

	return x.y<y.y;

}



bool cmp3(edge x,edge y){

	return x.w<y.w;

}



int getfa(int x){

	if(f[x]==x)return x;

	return f[x]=getfa(f[x]);

}



int main(){

	cin>>N;

	int i,j;

	for(i=1;i<=N;i++)

	f[i]=i;

	

	for(i=1;i<=N;i++){

		cin>>a[i].x>>a[i].y;

		a[i].id=i;

	}

	

	sort(a+1,a+N+1,cmp1);



	

	for(i=1;i<N;i++){

		e[i].x=a[i].id;

		e[i].y=a[i+1].id;

		e[i].w=a[i+1].x-a[i].x;

	}

	

	sort(a+1,a+N+1,cmp2);

	

	for(i=1;i<N;i++){

		e[i+N-1].x=a[i].id;

		e[i+N-1].y=a[i+1].id;

		e[i+N-1].w=a[i+1].y-a[i].y;

	}

	

	sort(e+1,e+2*N-1,cmp3);

	

	for(i=1;i<N;i++){

		while(p<2*N-2){

			p++;

			int fx=getfa(e[p].x),fy=getfa(e[p].y);

			if(fx!=fy){

				f[fy]=fx;

				cost+=e[p].w;

				break;

			}

		}

	}

	cout<<cost<<endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<fstream>

#include<sstream>

#include<algorithm>

#include<cstdio>

#include<cassert>

#include<cmath>

#include<cstdlib>

#include<cstring>

#include<string>

#include<deque>

#include<queue>

#include<vector>

#include<list>

#include<map>

#include<set>

#include<bitset>

#include<stack>

#include<iomanip>

#include<utility>

#include<functional>

using namespace std;

int n,m;

int ans;

int par[100005];

vector<int> edge[100005];

vector<pair<int,int> > el;

pair<int,int> e[200005];

pair<pair<int,int>,int> a[100005],b[100005];

int find(int x)

{

	if(par[x]!=x)

		return par[x]=find(par[x]);

	else

		return par[x];

}

bool unite(int x,int y)

{

	x=find(x);

	y=find(y);

	if(x==y)

		return false;

	else

	{

		par[x]=y;

		return true;

	}

}

int main()

{

	cin>>n;

	for(int i=0;i<n;i++)

	{

		cin>>a[i].first.first>>a[i].first.second;

		b[i].first.first=a[i].first.second;

		b[i].first.second=a[i].first.first;

		a[i].second=b[i].second=i;

	}

	sort(a,a+n);

	sort(b,b+n);

	for(int i=1;i<n;i++)

	{

		edge[a[i].second].push_back(a[i-1].second);

		e[m]=make_pair(a[i].second,a[i-1].second);

		el.push_back(make_pair(a[i].first.first-a[i-1].first.first,m++));

	}

	for(int i=1;i<n;i++)

	{

		edge[b[i].second].push_back(b[i-1].second);

		e[m]=make_pair(b[i].second,b[i-1].second);

		el.push_back(make_pair(b[i].first.first-b[i-1].first.first,m++));

	}

	for(int i=0;i<n;i++)

		par[i]=i;

	sort(el.begin(),el.end());

	for(int i=0;i<m;i++)

	{

		int xx=e[el[i].second].first;

		int yy=e[el[i].second].second;

		if(unite(xx,yy))

			ans+=el[i].first;

	}

	cout<<ans;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.52365918301133x):
#include <bits/stdc++.h>

using namespace std;

long long ans;

int n,fa[100003],r[100003];

pair<int,int> a[100003],x[100003],y[100003];

priority_queue <pair<int,pair<int,int> > > q;

int find_fa(int x){

	if(fa[x]==x) return x;

	return fa[x]=find_fa(fa[x]);

}

void uni(int x,int y){

	x=find_fa(x);

	y=find_fa(y);

	if (r[x]<r[y]) fa[x]=y;

	else{

		fa[y]=x;

		if(r[x]==r[y]) r[x]++;

	}

}

int main(){

	cin>>n;

	for(int i=0;i<n;i++) fa[i]=i;

	for(int i=0;i<n;i++){

		cin>>a[i].first>>a[i].second;

		x[i]={a[i].first,i};

		y[i]={a[i].second,i};

	}

	sort(x,x+n);

	sort(y,y+n);

	for(int i=0;i<n-1;i++){

		q.push({-(x[i+1].first-x[i].first),{x[i].second,x[i+1].second}});

		q.push({-(y[i+1].first-y[i].first),{y[i].second,y[i+1].second}});

	}

	while(q.size()){

		int c=-q.top().first,p1=q.top().second.first,p2=q.top().second.second;

		if(find_fa(p1)!=find_fa(p2)){

			uni(p1,p2);

			ans+=c;

		}

		q.pop();

	}

	cout<<ans;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<fstream>

#include<sstream>

#include<algorithm>

#include<cstdio>

#include<cassert>

#include<cmath>

#include<cstdlib>

#include<cstring>

#include<string>

#include<deque>

#include<queue>

#include<vector>

#include<list>

#include<map>

#include<set>

#include<bitset>

#include<stack>

#include<iomanip>

#include<utility>

#include<functional>

using namespace std;

int n,m;

int ans;

int par[100005];

vector<int> edge[100005];

vector<pair<int,int> > el;

pair<int,int> e[200005];

pair<pair<int,int>,int> a[100005],b[100005];

int find(int x)

{

	if(par[x]!=x)

		return par[x]=find(par[x]);

	else

		return par[x];

}

bool unite(int x,int y)

{

	x=find(x);

	y=find(y);

	if(x==y)

		return false;

	else

	{

		par[x]=y;

		return true;

	}

}

int main()

{

	cin>>n;

	for(int i=0;i<n;i++)

	{

		cin>>a[i].first.first>>a[i].first.second;

		b[i].first.first=a[i].first.second;

		b[i].first.second=a[i].first.first;

		a[i].second=b[i].second=i;

	}

	sort(a,a+n);

	sort(b,b+n);

	for(int i=1;i<n;i++)

	{

		edge[a[i].second].push_back(a[i-1].second);

		e[m]=make_pair(a[i].second,a[i-1].second);

		el.push_back(make_pair(a[i].first.first-a[i-1].first.first,m++));

	}

	for(int i=1;i<n;i++)

	{

		edge[b[i].second].push_back(b[i-1].second);

		e[m]=make_pair(b[i].second,b[i-1].second);

		el.push_back(make_pair(b[i].first.first-b[i-1].first.first,m++));

	}

	for(int i=0;i<n;i++)

		par[i]=i;

	sort(el.begin(),el.end());

	for(int i=0;i<m;i++)

	{

		int xx=e[el[i].second].first;

		int yy=e[el[i].second].second;

		if(unite(xx,yy))

			ans+=el[i].first;

	}

	cout<<ans;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.525783207144885x):
#include<iostream>

#include<cstdio>

#include<algorithm>

#define ll long long

#define N 100100

using namespace std;



ll n,fa[N],bb,ans;

struct Node

{

	ll x,y,id;

} node[N];

struct Bn

{

	ll a,b,c;

	bool operator < (const Bn u) const

	{

		return c<u.c;

	}

} bn[N<<1];



ll ff(ll u)

{

	return u==fa[u]?u:fa[u]=ff(fa[u]);

}

inline bool cmp1(Node u,Node v)

{

	return u.x<v.x;

}

inline bool cmp2(Node u,Node v)

{

	return u.y<v.y;

}



int main()

{

	ll i,j,p;

	cin>>n;

	for(i=1; i<=n; i++)

	{

		scanf("%lld%lld",&node[i].x,&node[i].y);

		node[i].id=i;

		fa[i]=i;

	}

	sort(node+1,node+n+1,cmp1);

	for(i=1; i<n; i++) bn[++bb].a=node[i].id,bn[bb].b=node[i+1].id,bn[bb].c=node[i+1].x-node[i].x;

	sort(node+1,node+n+1,cmp2);

	for(i=1; i<n; i++) bn[++bb].a=node[i].id,bn[bb].b=node[i+1].id,bn[bb].c=node[i+1].y-node[i].y;

	sort(bn+1,bn+bb+1);

	for(i=1; i<=bb; i++)

	{

		if(ff(bn[i].a)==ff(bn[i].b)) continue;

		fa[ff(bn[i].a)]=ff(bn[i].b);

		ans+=bn[i].c;

	}

	cout<<ans;

}
// Slower C++ solution:
#include<iostream>

#include<fstream>

#include<sstream>

#include<algorithm>

#include<cstdio>

#include<cassert>

#include<cmath>

#include<cstdlib>

#include<cstring>

#include<string>

#include<deque>

#include<queue>

#include<vector>

#include<list>

#include<map>

#include<set>

#include<bitset>

#include<stack>

#include<iomanip>

#include<utility>

#include<functional>

using namespace std;

int n,m;

int ans;

int par[100005];

vector<int> edge[100005];

vector<pair<int,int> > el;

pair<int,int> e[200005];

pair<pair<int,int>,int> a[100005],b[100005];

int find(int x)

{

	if(par[x]!=x)

		return par[x]=find(par[x]);

	else

		return par[x];

}

bool unite(int x,int y)

{

	x=find(x);

	y=find(y);

	if(x==y)

		return false;

	else

	{

		par[x]=y;

		return true;

	}

}

int main()

{

	cin>>n;

	for(int i=0;i<n;i++)

	{

		cin>>a[i].first.first>>a[i].first.second;

		b[i].first.first=a[i].first.second;

		b[i].first.second=a[i].first.first;

		a[i].second=b[i].second=i;

	}

	sort(a,a+n);

	sort(b,b+n);

	for(int i=1;i<n;i++)

	{

		edge[a[i].second].push_back(a[i-1].second);

		e[m]=make_pair(a[i].second,a[i-1].second);

		el.push_back(make_pair(a[i].first.first-a[i-1].first.first,m++));

	}

	for(int i=1;i<n;i++)

	{

		edge[b[i].second].push_back(b[i-1].second);

		e[m]=make_pair(b[i].second,b[i-1].second);

		el.push_back(make_pair(b[i].first.first-b[i-1].first.first,m++));

	}

	for(int i=0;i<n;i++)

		par[i]=i;

	sort(el.begin(),el.end());

	for(int i=0;i<m;i++)

	{

		int xx=e[el[i].second].first;

		int yy=e[el[i].second].second;

		if(unite(xx,yy))

			ans+=el[i].first;

	}

	cout<<ans;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.568157564260057x):
#include<bits/stdc++.h>

using namespace std;

struct gg{

	int a,b,id;

}node[100010],nw[100010];

struct hh{

	int u,v,w;

}mp[200010];

int n,num,fa[100010];

int find(int x)

{return fa[x]==x?x:fa[x]=find(fa[x]);}

bool cmp(gg mo,gg ha)

{return mo.a<ha.a;}

bool cmpp(gg mo,gg ha)

{return mo.b<ha.b;}

bool cmppp(hh mo,hh ha)

{return mo.w<ha.w;}

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

	{

		scanf("%d%d",&node[i].a,&node[i].b);

		node[i].id=i;nw[i]=node[i];

	}

	sort(nw+1,nw+n+1,cmp);

	sort(node+1,node+n+1,cmpp);

	for(int i=2;i<=n;i++)

	{

		mp[++num].u=nw[i-1].id;

		mp[num].v=nw[i].id;

		mp[num].w=nw[i].a-nw[i-1].a;

		mp[++num].u=node[i-1].id;

		mp[num].v=node[i].id;

		mp[num].w=node[i].b-node[i-1].b;

	}

	int ans=0,cnt=n-1;

	sort(mp+1,mp+num+1,cmppp);

	for(int i=1;i<=n;i++)fa[i]=i;

	for(int i=1;i<=num&&cnt>=1;i++)

	{

		if(find(mp[i].u)==find(mp[i].v))continue;

		else ans+=mp[i].w,fa[find(mp[i].u)]=find(mp[i].v),cnt--;

	}

	cout<<ans;

}