stablegradients/pie-gem5-pairs huggingface dataset:
// 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];



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;

	}

}



signed main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int n; cin >> n;

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

		cin >> 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());

	for (int i=0; i<100001; i++) up[i] = -1;

	long long ans = 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));

			ans += q[i].first;

		}

	}

	cout << ans;

}


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

#include<cmath>

#include<algorithm>

#include<cstdio>

#include<cstdlib>

using namespace std;



const int maxn=1e5+5;

int fa[maxn];

int tot;

struct way{

    int u,v;

	long long w;

}edge[maxn*4];

struct node

{

	int x,y,id;

}a[maxn];

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

void link(int x,int y)

{

    int tx=find(x),ty=find(y);

    fa[ty]=tx;

}

bool cmp(way a,way b){return a.w<b.w;}

bool cmp1(node a,node b){return a.x<b.x;}

bool cmp2(node a,node b){return a.y<b.y;}

int main()

{

    int m,n;

    scanf("%d",&n);

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

    {

    	a[i].id=i;

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

	}

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

	int cnt=0;

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

    {

    	cnt++;

    	edge[cnt].u=a[i].id;

    	edge[cnt].v=a[i+1].id;

    	edge[cnt].w=(long long)(a[i+1].x-a[i].x);

    	cnt++;

    	edge[cnt].u=a[i+1].id;

    	edge[cnt].v=a[i].id;

    	edge[cnt].w=(long long)(a[i+1].x-a[i].x);

	}

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

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

    {

    	cnt++;

    	edge[cnt].u=a[i].id;

    	edge[cnt].v=a[i+1].id;

    	edge[cnt].w=(long long)(a[i+1].y-a[i].y);

    	cnt++;

    	edge[cnt].u=a[i+1].id;

    	edge[cnt].v=a[i].id;

    	edge[cnt].w=(long long)(a[i+1].y-a[i].y);

	}

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

    sort(edge+1,edge+cnt+1,cmp);

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

    {

        if(find(edge[i].u)!=find(edge[i].v))

        {

            link(edge[i].u,edge[i].v);

            tot=tot+edge[i].w; 

        }

    }

    cout<<tot<<endl;

    return 0;

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

#include<cstring>

#include<algorithm>

#include<queue>

#include<cmath>

#include<iostream>

using namespace std;

const int inf=0x3f3f3f3f;

const int maxn=1e5+10;



struct edge{

    int u,v,w;

    edge(int _u=0,int _v=0,int _w=0):u(_u),v(_v),w(_w){

    }

    bool operator<(const edge & a)const{

        return w<a.w;

    }

}es[2*maxn];



struct point{

    int x,y,id;

    point(int _x=0,int _y=0,int _id=0):x(_x),y(_y),id(_id){}

} ps[maxn];



int n,cnt,f[maxn];



bool cmpx(point a,point b){

    return a.x<b.x;

}



bool cmpy(point a,point b){

    return a.y<b.y;

}



int findf(int a){

    return ((f[a]==-1)?a:f[a]=findf(f[a]));

}



int kruskal(){

    int i,ans=0,cc=0;

    memset(f,0xff,sizeof(f));

    sort(es,es+cnt);

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

        int u=es[i].u,v=es[i].v,w=es[i].w;

        int fu=findf(u),fv=findf(v);

        if(fu!=fv){

            ans+=w,++cc;

            f[fu]=fv;

        }

        if(cc==n-1)break;

    }

    if(cc<n-1)return -1;

    return ans;

}

int main(){

    int i,j,k,c,un,vn;

    scanf("%d",&n);

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

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

        ps[i].id=i;

    }

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

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

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

    }

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

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

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

    }

    printf("%d\n",kruskal());

    return 0;

}


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

#include<cmath>

#include<algorithm>

#include<cstdio>

#include<cstdlib>

using namespace std;



const int maxn=1e5+5;

int fa[maxn];

int tot;

struct way{

    int u,v;

	long long w;

}edge[maxn*4];

struct node

{

	int x,y,id;

}a[maxn];

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

void link(int x,int y)

{

    int tx=find(x),ty=find(y);

    fa[ty]=tx;

}

bool cmp(way a,way b){return a.w<b.w;}

bool cmp1(node a,node b){return a.x<b.x;}

bool cmp2(node a,node b){return a.y<b.y;}

int main()

{

    int m,n;

    scanf("%d",&n);

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

    {

    	a[i].id=i;

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

	}

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

	int cnt=0;

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

    {

    	cnt++;

    	edge[cnt].u=a[i].id;

    	edge[cnt].v=a[i+1].id;

    	edge[cnt].w=(long long)(a[i+1].x-a[i].x);

    	cnt++;

    	edge[cnt].u=a[i+1].id;

    	edge[cnt].v=a[i].id;

    	edge[cnt].w=(long long)(a[i+1].x-a[i].x);

	}

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

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

    {

    	cnt++;

    	edge[cnt].u=a[i].id;

    	edge[cnt].v=a[i+1].id;

    	edge[cnt].w=(long long)(a[i+1].y-a[i].y);

    	cnt++;

    	edge[cnt].u=a[i+1].id;

    	edge[cnt].v=a[i].id;

    	edge[cnt].w=(long long)(a[i+1].y-a[i].y);

	}

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

    sort(edge+1,edge+cnt+1,cmp);

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

    {

        if(find(edge[i].u)!=find(edge[i].v))

        {

            link(edge[i].u,edge[i].v);

            tot=tot+edge[i].w; 

        }

    }

    cout<<tot<<endl;

    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];



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;

	}

}



signed main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int n; cin >> n;

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

		cin >> 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());

	for (int i=0; i<100001; i++) up[i] = -1;

	long long ans = 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));

			ans += q[i].first;

		}

	}

	cout << ans;

}


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



using namespace std;

typedef long long ll;

struct Point

{

    ll x,y;

    int id;

};

struct edge

{

    int a,b,w;

};

bool cmp1(Point a,Point b)

{

    return a.x<b.x;

}

bool cmp2(Point a,Point b)

{

    return a.y<b.y;

}

bool cmp3(edge a,edge b)

{

    return a.w<b.w;

}

int f[100005];

int bcj(int p)

{

    if(f[p]==p)

        return p;

    f[p]=bcj(f[p]);

    return f[p];

}

void join(int p,int q)

{

    int fp=bcj(p);

    int fq=bcj(q);

    if(fp!=fq)

        f[fp]=fq;

}



Point village[100005];

edge e[200005];

int main()

{

    int N;



    scanf("%d",&N);

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

    {

        f[i]=i;

    }

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

    {

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

        village[i].id=i;

    }



    int cnt=0;

    sort(village,village+N,cmp1);

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

    {

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

        e[cnt].b=village[i+1].id;

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

        if(e[cnt].w<0)

            e[cnt].w*=-1;

        cnt++;

    }

    sort(village,village+N,cmp2);

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

    {

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

        e[cnt].b=village[i+1].id;

        e[cnt].w=village[i].y-village[i+1].y;

        if(e[cnt].w<0)

            e[cnt].w*=-1;

        cnt++;

    }



    sort(e,e+cnt,cmp3);

    ll ans=0;

    int p=0;

    while(N-->1)

    {

        while(1)

        {



            if(bcj(e[p].a)!=bcj(e[p].b))

            {

                join(e[p].a,e[p].b);

                ans+=(ll)e[p].w;



                p++;

                break;

            }

            p++;

        }

    }

    printf("%lld",ans);







}
// 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];



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;

	}

}



signed main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int n; cin >> n;

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

		cin >> 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());

	for (int i=0; i<100001; i++) up[i] = -1;

	long long ans = 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));

			ans += q[i].first;

		}

	}

	cout << ans;

}


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

#include <vector>

#include <algorithm>

#include <math.h>

using namespace std;



#define int long long

const int N = 1e5+5;

int n, ans = 0, par[N];



struct point

{

    int x,y,id;

};



struct edge

{

    int u,v,w;

};



bool cmp_x(point a, point b)

{

    return a.x < b.x;

}



bool cmp_y(point a, point b)

{

    return a.y < b.y;

}



bool cmp(edge a, edge b)

{

    return a.w < b.w;

}

int dist(point a, point b)

{

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

}



void init()

{

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

        par[i] = i;

}



int findSet(int i)

{

    if (i == par[i])

        return i;

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

}



bool isSameSet(int i, int j)

{

    return (findSet(i) == findSet(j));

}



void unionSet(int i, int j)

{

    if (!isSameSet(i,j))

    {

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

    }

}



point tmp_x[N], tmp_y[N];

vector<edge> ve;



signed main()

{

    int x,y;

    cin >> n;

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

    {

        cin >> x >> y;

        tmp_x[i] = tmp_y[i] = {x,y};

        tmp_x[i].id = i;

        tmp_y[i].id = i;

    }

    sort(tmp_x,tmp_x+n,cmp_x);

    sort(tmp_y,tmp_y+n,cmp_y);

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

    {

        ve.push_back({tmp_x[i].id, tmp_x[i+1].id, dist(tmp_x[i],tmp_x[i+1])});

        ve.push_back({tmp_y[i].id, tmp_y[i+1].id, dist(tmp_y[i],tmp_y[i+1])});

    }

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

    init();

    for (auto e: ve)

    {

        int u = e.u, v = e.v, w = e.w;

        if (!isSameSet(u,v))

        {

            unionSet(u,v);

            ans += w;

        }

    }

    cout << ans;

}
// 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];



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;

	}

}



signed main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int n; cin >> n;

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

		cin >> 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());

	for (int i=0; i<100001; i++) up[i] = -1;

	long long ans = 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));

			ans += q[i].first;

		}

	}

	cout << ans;

}


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

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;



struct Node {

    int x, y, id;

}p[N];

bool cmp1(Node a, Node b) {

    return a.x < b.x;

}

bool cmp2(Node a, Node b) {

    return a.y < b.y;

}

struct Edge {

    int u, v, w;

    bool operator < (const Edge &t) const {

        return w < t.w;

    }

}e[N << 1];

int fa[N];



inline int getFa(int u) {

    return fa[u] == 0 ? u : fa[u] = getFa(fa[u]);

}



int main() {

#ifdef purple_bro

    freopen("in.txt", "r", stdin);

#endif // purple_bro

    int n;



    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, cmp1);



    int tot = 0;



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

        e[++tot] = {p[i].id, p[i + 1].id, p[i + 1].x - p[i].x};



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



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

        e[++tot] = {p[i].id, p[i + 1].id, p[i + 1].y - p[i].y};



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



    LL ans = 0;

    int cnt = 0;



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

        int u = e[i].u;

        int v = e[i].v;

        int w = e[i].w;

        int du = getFa(u);

        int dv = getFa(v);

        if (du != dv) {

            fa[du] = dv;

            cnt++;

            ans += w;

        }

    }



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



    return 0;

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

#include<cstring>

#include<algorithm>

#include<queue>

#include<cmath>

#include<iostream>

using namespace std;

const int inf=0x3f3f3f3f;

const int maxn=1e5+10;



struct edge{

    int u,v,w;

    edge(int _u=0,int _v=0,int _w=0):u(_u),v(_v),w(_w){

    }

    bool operator<(const edge & a)const{

        return w<a.w;

    }

}es[2*maxn];



struct point{

    int x,y,id;

    point(int _x=0,int _y=0,int _id=0):x(_x),y(_y),id(_id){}

} ps[maxn];



int n,cnt,f[maxn];



bool cmpx(point a,point b){

    return a.x<b.x;

}



bool cmpy(point a,point b){

    return a.y<b.y;

}



int findf(int a){

    return ((f[a]==-1)?a:f[a]=findf(f[a]));

}



int kruskal(){

    int i,ans=0,cc=0;

    memset(f,0xff,sizeof(f));

    sort(es,es+cnt);

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

        int u=es[i].u,v=es[i].v,w=es[i].w;

        int fu=findf(u),fv=findf(v);

        if(fu!=fv){

            ans+=w,++cc;

            f[fu]=fv;

        }

        if(cc==n-1)break;

    }

    if(cc<n-1)return -1;

    return ans;

}

int main(){

    int i,j,k,c,un,vn;

    scanf("%d",&n);

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

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

        ps[i].id=i;

    }

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

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

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

    }

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

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

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

    }

    printf("%d\n",kruskal());

    return 0;

}


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

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;



struct Node {

    int x, y, id;

}p[N];

bool cmp1(Node a, Node b) {

    return a.x < b.x;

}

bool cmp2(Node a, Node b) {

    return a.y < b.y;

}

struct Edge {

    int u, v, w;

    bool operator < (const Edge &t) const {

        return w < t.w;

    }

}e[N << 1];

int fa[N];



inline int getFa(int u) {

    return fa[u] == 0 ? u : fa[u] = getFa(fa[u]);

}



int main() {

#ifdef purple_bro

    freopen("in.txt", "r", stdin);

#endif // purple_bro

    int n;



    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, cmp1);



    int tot = 0;



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

        e[++tot] = {p[i].id, p[i + 1].id, p[i + 1].x - p[i].x};



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



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

        e[++tot] = {p[i].id, p[i + 1].id, p[i + 1].y - p[i].y};



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



    LL ans = 0;

    int cnt = 0;



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

        int u = e[i].u;

        int v = e[i].v;

        int w = e[i].w;

        int du = getFa(u);

        int dv = getFa(v);

        if (du != dv) {

            fa[du] = dv;

            cnt++;

            ans += w;

        }

    }



    printf("%lld\n", 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];



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;

	}

}



signed main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int n; cin >> n;

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

		cin >> 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());

	for (int i=0; i<100001; i++) up[i] = -1;

	long long ans = 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));

			ans += q[i].first;

		}

	}

	cout << ans;

}


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

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

#define mst(a,b) memset(a,b,sizeof(a))

#define lowbit(x) ((x)&(-x))

#define X first

#define Y second

using namespace std;

using namespace __gnu_cxx;

using namespace __gnu_pbds;

typedef long long LL;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

const int inf = 0x3f3f3f3f;

const ll INF = 0x3f3f3f3f3f3f3f3f;

const int maxn = 100000+10;

const int maxm = 200000+10;

const int mod = 1e9+7;

const double eps = 1e-9;

struct Edge{

    int u,v,w;

    Edge(){}

    bool operator < (const Edge &rhs){

        return w<rhs.w;

    }

}edge[maxn<<1];

pii x[maxn],y[maxn];

int fa[maxn];

int Find(int u){

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

}

int main() {

#ifdef local

    freopen("in.txt", "r", stdin);

//    freopen("out.txt", "w", stdout);

#endif

    ios::sync_with_stdio(0);

    cin.tie(0);cout.tie(0);

    int n; cin>>n;

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

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

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

        fa[i]=i;

    }

    sort(x,x+n);

    sort(y,y+n);

    int tot=0;

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

        edge[tot].u=x[i-1].Y; edge[tot].v=x[i].Y; edge[tot++].w=x[i].X-x[i-1].X;

        edge[tot].u=y[i-1].Y; edge[tot].v=y[i].Y; edge[tot++].w=y[i].X-y[i-1].X;

    }

    sort(edge,edge+tot);

    int ans=0;

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

        int u=Find(edge[i].u),v=Find(edge[i].v);

        if (u!=v){

            ans+=edge[i].w;

            fa[u]=v;

        }

    }

    cout<<ans<<"\n";

    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];



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;

	}

}



signed main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int n; cin >> n;

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

		cin >> 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());

	for (int i=0; i<100001; i++) up[i] = -1;

	long long ans = 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));

			ans += q[i].first;

		}

	}

	cout << ans;

}


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

#include <algorithm>

#include <vector>

#include <queue>

#include <functional>



using namespace std;



typedef pair<int,int> PII;

typedef pair<int,PII> PIII;



int ufind(vector<int>& P, int u) {

  if (P[u] == 0) {

    return u;

  }

  return P[u] = ufind(P, P[u]);

}



void uf(vector<int>& P, int u, int v) {

  int _u = ufind(P, u);

  int _v = ufind(P, v);

  if (_u != _v) {

    P[_u] = _v;

  }

}



int main()

{

  int n;

  vector<PII> X, Y;



  cin >> n;

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

    int x,y;

    cin >> x >> y;

    X.push_back(make_pair(x,i));

    Y.push_back(make_pair(y,i));

  }



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

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



  vector<PIII> q;

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

    int u = X[i-1].second, v = X[i].second;

    int xu = X[i-1].first, xv = X[i].first;

    q.push_back(make_pair(xv-xu, make_pair(u,v)));

  }

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

    int u = Y[i-1].second, v = Y[i].second;

    int yu = Y[i-1].first, yv = Y[i].first;

    q.push_back(make_pair(yv-yu, make_pair(u,v)));

  }

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



  long long res = 0;

  vector<int> P(n+1);

  for (PIII p : q) {

    int d = p.first;

    int u = p.second.first, v = p.second.second;



    if (ufind(P, u) != ufind(P, v)) {

      res += d;

      uf(P, u, v);

    }

  }

  cout << res << "\n";

}