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

using namespace std;

#define int long long

vector<pair<int,pair<int,int> > >points;

vector<pair<int,pair<int,int> > > edge;

int par[100005];

bool cmp(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

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

}

bool cmp2(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

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

}

void init(){

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

		par[i]=i;

	}

}

int find(int i){

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

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

}

void dsu(int i,int j){

	par[find(i)]=find(j);

}

bool same(int i,int j){

	return find(i)==find(j);

}

signed main(){

//	freopen("plantree.inp","w",stdin);

//	freopen("plantree.out","r",stdout);

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

	int n;

	cin>>n;

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

		int x,y;

		cin>>x>>y;

		points.push_back(make_pair(i,make_pair(x,y)));

	}	

	init();

	sort(points.begin(),points.end(),cmp);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.first-points[i-1].second.first,make_pair(points[i].first,points[i-1].first)));

	}

	sort(points.begin(),points.end(),cmp2);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.second-points[i-1].second.second,make_pair(points[i].first,points[i-1].first)));

	}

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

	int ans=0;

	

	for(int i=0;i<edge.size();i++){

		if(!same(edge[i].second.second,edge[i].second.first)){

			ans+=edge[i].first;

			dsu(edge[i].second.second,edge[i].second.first);

		}

	}

	cout<<ans;

}

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

using namespace std;

int f[100005];

int n;

int cnt;

struct house

{

	int x,y;

	int id;

};

house a[100005];

struct edge

{

	int x,y;

	int val;

};

edge e[100005<<2];

int find(int x)

{

	return x==f[x]? x : f[x]=find(f[x]);

}

bool cmp(house a,house b)

{

	return a.x<b.x;

}

bool cmp2(house a,house b)

{

	return a.y<b.y;

}

bool cmp3(edge a,edge b)

{

	return a.val<b.val;

}

int main()

{

	cin>>n;

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

	{

		f[i]=i;

	}

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

	{

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

		a[i].id=i;

	}

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

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

	{

		cnt++;

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

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

		e[cnt].val=abs(a[i].x-a[i+1].x);

	}

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

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

	{

		cnt++;

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

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

		e[cnt].val=abs(a[i].y-a[i+1].y);

	}

	sort(e+1,e+cnt+1,cmp3);

	int p=0;

	long long ans=0;

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

	{

		int fx=find(e[i].x);

		int fy=find(e[i].y);

		if(fx==fy)

		{

			continue;

		}

		f[fx]=fy;

		ans+=e[i].val;

		p++;

		if(p==n-1)

		{

			break;

		}

	}

	cout<<ans;

	return 0;

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



using namespace std;



struct point

{

	int x, y, id;

} p[100005];



vector< pair<int, pair<int, int> > > q;



bool cmpx(point a, point b)

{

	if (a.x != b.x) return a.x > b.x;

	else return a.y > b.y;

}



bool cmpy(point a, point b)

{

	if (a.y != b.y) return a.y > b.y;

	else return a.x > b.x;

}



int up[100005];



void makesets()

{

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

	{

		up[i] = -1;

	}

}



int Find(int x)

{

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

	int root = Find(up[x]);

	up[x] = root;

	return root;

}



void Union(int x, int y)

{

	if (Find(x) == Find(y)) return;

	if (up[x] < up[y])

	{

		up[x] += up[y];

		up[y] = x;

	}

	else

	{

		up[y] += up[x];

		up[x] = y;

	}

}



int n;



signed main()

{

	scanf("%d", &n);

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

	{

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

		p[i].id = i;

	}

	sort(p+1, p+1+n, cmpx);

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

	{

		q.push_back(make_pair(p[i].x - p[i+1].x, make_pair(p[i].id, p[i+1].id)));

	}

	sort(p+1, p+1+n, cmpy);

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

	{

		q.push_back(make_pair(p[i].y - p[i+1].y, make_pair(p[i].id, p[i+1].id)));

	}

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

	makesets();

	long long res = 0;

	for (int i = 0; i < q.size(); i++)

	{

		int u = q[i].second.first, v = q[i].second.second;

		if (Find(u) != Find(v))

		{

			Union(Find(u), Find(v));

			res += q[i].first;

		}

	}

	printf("%lld", res);

}

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

#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <string>

#include <stack>

#include <queue>

#include <map>

#include <set>

#include <vector>

#include <sstream>

using namespace std;



const int inf=0x3f3f3f3f;

const int maxn=100005;

int n, fa[maxn], cnt;



struct edge{

    int u, v, cost;

    edge(int cu, int cv, int ccost){u=cu, v=cv, cost=ccost;}

    edge(){}

    bool operator<(const edge &cmp) const{

    	return cost<cmp.cost;

    }

}E[maxn*4];



struct point{

    int x, y, id;

}P[maxn];



bool cmpx(point c1, point c2){return c1.x<c2.x;}

bool cmpy(point c1, point c2){return c1.y<c2.y;}



int Find(int x){

    return x==fa[x]?x:fa[x]=Find(fa[x]);

}

 

long long Kruskal(){

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

    sort(E, E+cnt);

    long long ans=0;

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

        int fu=Find(E[i].u);

        int fv=Find(E[i].v);

        if(fu!=fv){

            ans+=E[i].cost;

            fa[fu]=fv;

        }

    }

    return ans;

}



int main(){

    cin>>n;

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

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

        P[i].id=i;

    }

    cnt=0;

    sort(P, P+n, cmpx);

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

        E[cnt++]=edge(P[i-1].id, P[i].id, P[i].x-P[i-1].x);

    }

    sort(P, P+n, cmpy);

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

        E[cnt++]=edge(P[i-1].id, P[i].id, P[i].y-P[i-1].y);

    }

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

    return 0;

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

using namespace std;

#define int long long

vector<pair<int,pair<int,int> > >points;

vector<pair<int,pair<int,int> > > edge;

int par[100005];

bool cmp(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

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

}

bool cmp2(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

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

}

void init(){

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

		par[i]=i;

	}

}

int find(int i){

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

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

}

void dsu(int i,int j){

	par[find(i)]=find(j);

}

bool same(int i,int j){

	return find(i)==find(j);

}

signed main(){

//	freopen("plantree.inp","w",stdin);

//	freopen("plantree.out","r",stdout);

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

	int n;

	cin>>n;

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

		int x,y;

		cin>>x>>y;

		points.push_back(make_pair(i,make_pair(x,y)));

	}	

	init();

	sort(points.begin(),points.end(),cmp);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.first-points[i-1].second.first,make_pair(points[i].first,points[i-1].first)));

	}

	sort(points.begin(),points.end(),cmp2);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.second-points[i-1].second.second,make_pair(points[i].first,points[i-1].first)));

	}

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

	int ans=0;

	

	for(int i=0;i<edge.size();i++){

		if(!same(edge[i].second.second,edge[i].second.first)){

			ans+=edge[i].first;

			dsu(edge[i].second.second,edge[i].second.first);

		}

	}

	cout<<ans;

}

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

#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <string>

#include <stack>

#include <queue>

#include <map>

#include <set>

#include <vector>

#include <sstream>

using namespace std;



const int inf=0x3f3f3f3f;

const int maxn=100005;

int n, fa[maxn], cnt;



struct edge{

    int u, v, cost;

    edge(int cu, int cv, int ccost){u=cu, v=cv, cost=ccost;}

    edge(){}

    bool operator<(const edge &cmp) const{

    	return cost<cmp.cost;

    }

}E[maxn*4];



struct point{

    int x, y, id;

}P[maxn];



bool cmpx(point c1, point c2){return c1.x<c2.x;}

bool cmpy(point c1, point c2){return c1.y<c2.y;}



int Find(int x){

    return x==fa[x]?x:fa[x]=Find(fa[x]);

}

 

long long Kruskal(){

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

    sort(E, E+cnt);

    long long ans=0;

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

        int fu=Find(E[i].u);

        int fv=Find(E[i].v);

        if(fu!=fv){

            ans+=E[i].cost;

            fa[fu]=fv;

        }

    }

    return ans;

}



int main(){

    cin>>n;

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

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

        P[i].id=i;

    }

    cnt=0;

    sort(P, P+n, cmpx);

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

        E[cnt++]=edge(P[i-1].id, P[i].id, P[i].x-P[i-1].x);

    }

    sort(P, P+n, cmpy);

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

        E[cnt++]=edge(P[i-1].id, P[i].id, P[i].y-P[i-1].y);

    }

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

    return 0;

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



using namespace std;



struct point

{

	int x, y, id;

} p[100005];



vector< pair<int, pair<int, int> > > q;



bool cmpx(point a, point b)

{

	if (a.x != b.x) return a.x > b.x;

	else return a.y > b.y;

}



bool cmpy(point a, point b)

{

	if (a.y != b.y) return a.y > b.y;

	else return a.x > b.x;

}



int up[100005];



void makesets()

{

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

	{

		up[i] = -1;

	}

}



int Find(int x)

{

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

	int root = Find(up[x]);

	up[x] = root;

	return root;

}



void Union(int x, int y)

{

	if (Find(x) == Find(y)) return;

	if (up[x] < up[y])

	{

		up[x] += up[y];

		up[y] = x;

	}

	else

	{

		up[y] += up[x];

		up[x] = y;

	}

}



int n;



signed main()

{

	scanf("%d", &n);

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

	{

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

		p[i].id = i;

	}

	sort(p+1, p+1+n, cmpx);

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

	{

		q.push_back(make_pair(p[i].x - p[i+1].x, make_pair(p[i].id, p[i+1].id)));

	}

	sort(p+1, p+1+n, cmpy);

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

	{

		q.push_back(make_pair(p[i].y - p[i+1].y, make_pair(p[i].id, p[i+1].id)));

	}

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

	makesets();

	long long res = 0;

	for (int i = 0; i < q.size(); i++)

	{

		int u = q[i].second.first, v = q[i].second.second;

		if (Find(u) != Find(v))

		{

			Union(Find(u), Find(v));

			res += q[i].first;

		}

	}

	printf("%lld", res);

}

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

using namespace std;

long long aa;bool bb;char ch;

long long scan()

{

	while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-');

	ch=='-'?(bb=1,aa=0):(bb=0,aa=ch-48);

	while(ch=getchar(),(ch>='0'&&ch<='9'))

		aa=aa*10+ch-48;

	return (bb?(-aa):(aa));

}

struct P

{

	int x,y,id;

} p[100010];

struct E

{

	int from,to,w;

} e[200010];

int tot=0,N,u,v,x,y,fu,fv;

long long ans=0;

int fa[100010];

bool cmpx(P u,P v){return u.x<v.x;}

bool cmpy(P u,P v){return u.y<v.y;}

bool cmp(E u,E v){return u.w<v.w;}

int find(int u)

{

	if(u!=fa[u])

		return fa[u]=find(fa[u]);

	return u;

}

int main()

{

	scanf("%d",&N);

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

	{

		fa[i]=i;

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

		p[i].x=x;

		p[i].y=y;

		p[i].id=i;

	}

	sort(p+1,p+N+1,cmpx);

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

	{

		e[++tot].from=p[i].id;

		e[tot].to=p[i+1].id;

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

	}

	sort(p+1,p+N+1,cmpy);

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

	{

		e[++tot].from=p[i].id;

		e[tot].to=p[i+1].id;

		e[tot].w=p[i+1].y-p[i].y;

	}

	sort(e+1,e+tot+1,cmp);

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

	{

		u=e[i].from;

		v=e[i].to;

		fu=find(u);

		fv=find(v);

		if(fu!=fv)

		{

			fa[fu]=fv;

			ans+=e[i].w;

			//cout<<u<<" "<<v<<" "<<e[i].w<<endl;

		}

	}

	cout<<ans<<endl;

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

using namespace std;

#define int long long

vector<pair<int,pair<int,int> > >points;

vector<pair<int,pair<int,int> > > edge;

int par[100005];

bool cmp(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

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

}

bool cmp2(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

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

}

void init(){

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

		par[i]=i;

	}

}

int find(int i){

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

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

}

void dsu(int i,int j){

	par[find(i)]=find(j);

}

bool same(int i,int j){

	return find(i)==find(j);

}

signed main(){

//	freopen("plantree.inp","w",stdin);

//	freopen("plantree.out","r",stdout);

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

	int n;

	cin>>n;

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

		int x,y;

		cin>>x>>y;

		points.push_back(make_pair(i,make_pair(x,y)));

	}	

	init();

	sort(points.begin(),points.end(),cmp);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.first-points[i-1].second.first,make_pair(points[i].first,points[i-1].first)));

	}

	sort(points.begin(),points.end(),cmp2);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.second-points[i-1].second.second,make_pair(points[i].first,points[i-1].first)));

	}

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

	int ans=0;

	

	for(int i=0;i<edge.size();i++){

		if(!same(edge[i].second.second,edge[i].second.first)){

			ans+=edge[i].first;

			dsu(edge[i].second.second,edge[i].second.first);

		}

	}

	cout<<ans;

}

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

using namespace std;

long long aa;bool bb;char ch;

long long scan()

{

	while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-');

	ch=='-'?(bb=1,aa=0):(bb=0,aa=ch-48);

	while(ch=getchar(),(ch>='0'&&ch<='9'))

		aa=aa*10+ch-48;

	return (bb?(-aa):(aa));

}

struct P

{

	int x,y,id;

} p[100010];

struct E

{

	int from,to,w;

} e[200010];

int tot=0,N,u,v,x,y,fu,fv;

long long ans=0;

int fa[100010];

bool cmpx(P u,P v){return u.x<v.x;}

bool cmpy(P u,P v){return u.y<v.y;}

bool cmp(E u,E v){return u.w<v.w;}

int find(int u)

{

	if(u!=fa[u])

		return fa[u]=find(fa[u]);

	return u;

}

int main()

{

	scanf("%d",&N);

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

	{

		fa[i]=i;

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

		p[i].x=x;

		p[i].y=y;

		p[i].id=i;

	}

	sort(p+1,p+N+1,cmpx);

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

	{

		e[++tot].from=p[i].id;

		e[tot].to=p[i+1].id;

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

	}

	sort(p+1,p+N+1,cmpy);

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

	{

		e[++tot].from=p[i].id;

		e[tot].to=p[i+1].id;

		e[tot].w=p[i+1].y-p[i].y;

	}

	sort(e+1,e+tot+1,cmp);

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

	{

		u=e[i].from;

		v=e[i].to;

		fu=find(u);

		fv=find(v);

		if(fu!=fv)

		{

			fa[fu]=fv;

			ans+=e[i].w;

			//cout<<u<<" "<<v<<" "<<e[i].w<<endl;

		}

	}

	cout<<ans<<endl;

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

using namespace std;

#define int long long

vector<pair<int,pair<int,int> > >points;

vector<pair<int,pair<int,int> > > edge;

int par[100005];

bool cmp(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

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

}

bool cmp2(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

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

}

void init(){

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

		par[i]=i;

	}

}

int find(int i){

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

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

}

void dsu(int i,int j){

	par[find(i)]=find(j);

}

bool same(int i,int j){

	return find(i)==find(j);

}

signed main(){

//	freopen("plantree.inp","w",stdin);

//	freopen("plantree.out","r",stdout);

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

	int n;

	cin>>n;

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

		int x,y;

		cin>>x>>y;

		points.push_back(make_pair(i,make_pair(x,y)));

	}	

	init();

	sort(points.begin(),points.end(),cmp);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.first-points[i-1].second.first,make_pair(points[i].first,points[i-1].first)));

	}

	sort(points.begin(),points.end(),cmp2);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.second-points[i-1].second.second,make_pair(points[i].first,points[i-1].first)));

	}

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

	int ans=0;

	

	for(int i=0;i<edge.size();i++){

		if(!same(edge[i].second.second,edge[i].second.first)){

			ans+=edge[i].first;

			dsu(edge[i].second.second,edge[i].second.first);

		}

	}

	cout<<ans;

}

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

#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;

const int maxN=1e5+5;



int fa[maxN];



struct node

{

    int x,y,id;

}a[maxN],b[maxN];



struct edge

{

    int f,t,w;

}e[maxN*2];



bool cmp1(node x1,node x2)

{

    return x1.x<x2.x;

}



bool cmp2(node x1,node x2)

{

    return x1.y<x2.y;

}



bool cmp3(edge x1,edge x2)

{

    return x1.w<x2.w;

}



int find(int x)

{

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

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

}



int uni(int f,int t)

{

    f=find(f);t=find(t);

    if(f==t)return 0;

    fa[f]=t;return 1;

}



int main()

{

    int k,n,cnt=0,now=0;

    long long ans=0;

    memset(fa,-1,sizeof(fa));

    scanf("%d",&n);

    for(k=0;k<n;k++)

    {

        scanf("%d%d",&a[k].x,&a[k].y);

        a[k].id=k;b[k].id=k;

        b[k].x=a[k].x;

        b[k].y=a[k].y;

    }

    sort(a,a+n,cmp1);

    sort(b,b+n,cmp2);

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

    {

        e[cnt].f=a[k-1].id;

        e[cnt].t=a[k].id;

        e[cnt].w=a[k].x-a[k-1].x;

        cnt++;

    }

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

    {

        e[cnt].f=b[k-1].id;

        e[cnt].t=b[k].id;

        e[cnt].w=b[k].y-b[k-1].y;

        cnt++;

    }

    sort(e,e+cnt,cmp3);

    for(k=0;k<cnt;k++)

    {

        if(uni(e[k].f,e[k].t))

        {

            ans+=e[k].w;

            now++;

        }

        if(now==n-1)break;

    }

    cout<<ans;

    return 0;

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



using namespace std;



struct point

{

	int x, y, id;

} p[100005];



vector< pair<int, pair<int, int> > > q;



bool cmpx(point a, point b)

{

	if (a.x != b.x) return a.x > b.x;

	else return a.y > b.y;

}



bool cmpy(point a, point b)

{

	if (a.y != b.y) return a.y > b.y;

	else return a.x > b.x;

}



int up[100005];



void makesets()

{

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

	{

		up[i] = -1;

	}

}



int Find(int x)

{

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

	int root = Find(up[x]);

	up[x] = root;

	return root;

}



void Union(int x, int y)

{

	if (Find(x) == Find(y)) return;

	if (up[x] < up[y])

	{

		up[x] += up[y];

		up[y] = x;

	}

	else

	{

		up[y] += up[x];

		up[x] = y;

	}

}



int n;



signed main()

{

	scanf("%d", &n);

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

	{

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

		p[i].id = i;

	}

	sort(p+1, p+1+n, cmpx);

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

	{

		q.push_back(make_pair(p[i].x - p[i+1].x, make_pair(p[i].id, p[i+1].id)));

	}

	sort(p+1, p+1+n, cmpy);

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

	{

		q.push_back(make_pair(p[i].y - p[i+1].y, make_pair(p[i].id, p[i+1].id)));

	}

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

	makesets();

	long long res = 0;

	for (int i = 0; i < q.size(); i++)

	{

		int u = q[i].second.first, v = q[i].second.second;

		if (Find(u) != Find(v))

		{

			Union(Find(u), Find(v));

			res += q[i].first;

		}

	}

	printf("%lld", res);

}

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

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<iostream>

#define rep(i,x,y) for(int i=x;i<=y;++i)



const int INF=1<<30;

using namespace std;



const int maxn=100000+10;



int p[maxn];

int findset(int x){return p[x]==x?x:p[x]=findset(p[x]);}

int merge(int a,int b)

{

	a=findset(a);b=findset(b);

	if(a==b) return 0;

	p[a]=b;return 1;

}



struct Edge{

	int u,v,w;

	Edge(int u=0,int v=0,int w=0):u(u),v(v),w(w){}

	inline bool operator < (const Edge &rhs)const{

		return w<rhs.w;

	}

};



int idx[maxn],idy[maxn],x[maxn],y[maxn];



bool cmpx(int a,int b){return x[a]<x[b];}

bool cmpy(int a,int b){return y[a]<y[b];}



vector<Edge>e;



int main()

{

	int n;cin>>n;

	rep(i,1,n) scanf("%d%d",x+i,y+i),idx[i]=idy[i]=i;

	sort(idx+1,idx+n+1,cmpx);

	sort(idy+1,idy+n+1,cmpy);



	rep(i,1,n-1)

	{

		e.push_back(Edge(idx[i],idx[i+1],x[idx[i+1]]-x[idx[i]]));

		e.push_back(Edge(idy[i],idy[i+1],y[idy[i+1]]-y[idy[i]]));

	}



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



	rep(i,1,n) p[i]=i;



	int cnt=0;long long cost=0;

	for(int i=0;i<(int)e.size();++i)

	{

	//	printf("(%d,%d,%d)\n",e[i].u,e[i].v,e[i].w);

		if(merge(e[i].u,e[i].v))

		{

			cost+=e[i].w;

			if(--cnt==1) break;

		}

	}



	cout<<cost<<endl;

	return 0;

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

using namespace std;

#define int long long

vector<pair<int,pair<int,int> > >points;

vector<pair<int,pair<int,int> > > edge;

int par[100005];

bool cmp(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

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

}

bool cmp2(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

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

}

void init(){

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

		par[i]=i;

	}

}

int find(int i){

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

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

}

void dsu(int i,int j){

	par[find(i)]=find(j);

}

bool same(int i,int j){

	return find(i)==find(j);

}

signed main(){

//	freopen("plantree.inp","w",stdin);

//	freopen("plantree.out","r",stdout);

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

	int n;

	cin>>n;

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

		int x,y;

		cin>>x>>y;

		points.push_back(make_pair(i,make_pair(x,y)));

	}	

	init();

	sort(points.begin(),points.end(),cmp);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.first-points[i-1].second.first,make_pair(points[i].first,points[i-1].first)));

	}

	sort(points.begin(),points.end(),cmp2);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.second-points[i-1].second.second,make_pair(points[i].first,points[i-1].first)));

	}

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

	int ans=0;

	

	for(int i=0;i<edge.size();i++){

		if(!same(edge[i].second.second,edge[i].second.first)){

			ans+=edge[i].first;

			dsu(edge[i].second.second,edge[i].second.first);

		}

	}

	cout<<ans;

}

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

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<iostream>

#define rep(i,x,y) for(int i=x;i<=y;++i)



const int INF=1<<30;

using namespace std;



const int maxn=100000+10;



int p[maxn];

int findset(int x){return p[x]==x?x:p[x]=findset(p[x]);}

int merge(int a,int b)

{

	a=findset(a);b=findset(b);

	if(a==b) return 0;

	p[a]=b;return 1;

}



struct Edge{

	int u,v,w;

	Edge(int u=0,int v=0,int w=0):u(u),v(v),w(w){}

	inline bool operator < (const Edge &rhs)const{

		return w<rhs.w;

	}

};



int idx[maxn],idy[maxn],x[maxn],y[maxn];



bool cmpx(int a,int b){return x[a]<x[b];}

bool cmpy(int a,int b){return y[a]<y[b];}



vector<Edge>e;



int main()

{

	int n;cin>>n;

	rep(i,1,n) scanf("%d%d",x+i,y+i),idx[i]=idy[i]=i;

	sort(idx+1,idx+n+1,cmpx);

	sort(idy+1,idy+n+1,cmpy);



	rep(i,1,n-1)

	{

		e.push_back(Edge(idx[i],idx[i+1],x[idx[i+1]]-x[idx[i]]));

		e.push_back(Edge(idy[i],idy[i+1],y[idy[i+1]]-y[idy[i]]));

	}



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



	rep(i,1,n) p[i]=i;



	int cnt=0;long long cost=0;

	for(int i=0;i<(int)e.size();++i)

	{

	//	printf("(%d,%d,%d)\n",e[i].u,e[i].v,e[i].w);

		if(merge(e[i].u,e[i].v))

		{

			cost+=e[i].w;

			if(--cnt==1) break;

		}

	}



	cout<<cost<<endl;

	return 0;

}