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



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.3773997145548085x):
#include<bits/stdc++.h>

using namespace std;



#define int long long

#define pb push_back

#define fi first

#define se second

#define FOR(a, b, c) for(int a = b; a <= c; ++a)

#define mp make_pair

const int N=1e5+5;

typedef pair<int,int> ii;

typedef pair<int,ii> iii;

int n, pr[N];

vector<ii> xx, yy;

priority_queue<iii, vector<iii>, greater<iii> > pq;

int find(int i) {

	return (pr[i] == i) ? i : (pr[i] = find(pr[i]));

}

signed main(){

    ios_base::sync_with_stdio(false);

    cin >> n;

    FOR(i, 1, n) {

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

    	xx.pb(mp(x, i)); yy.pb(mp(y, i));

	}

	sort(xx.begin(), xx.end()); sort(yy.begin(), yy.end());

	FOR(i, 1, xx.size() - 1)	{

		pq.push(mp(xx[i].fi - xx[i - 1].fi, mp(xx[i].se, xx[i - 1].se)) );

	}

	FOR(i, 1, yy.size() - 1)

		pq.push(mp(yy[i].fi - yy[i - 1].fi, mp(yy[i].se, yy[i - 1].se)));

	int ans = 0;

	FOR(i, 1, n) 	pr[i] = i;

	while(!pq.empty())	{

		int u = pq.top().se.fi, v = pq.top().se.se, uv = pq.top().fi; pq.pop();

		if(find(u) != find(v))	{

			pr[find(u)] = find(v);

			ans += uv;

		}

	}

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



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.4142556610112225x):
#include<bits/stdc++.h>

#define mp make_pair

#define pb push_back

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef vector<int > vi;

typedef pair<int ,int > pii;

typedef vector<pii> vii;

const int inf=0x3f3f3f3f, maxn=100007, mod=1e9+7;

const ll linf=0x3f3f3f3f3f3f3f3fLL;

const ll P=19260817;

int fa[100007];

int n;

int find(int x){

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

}

struct node{

	int x,y,id;

}a[100007];

bool cmp1(const node& a,const node& b){

	return a.x<b.x;

}

bool cmp2(const node& a,const node& b){

	return a.y<b.y;

}

struct edge{

	int x,y,val;

}e[100005<<2];

bool cmp3(const edge& a,const edge &b){

	return a.val<b.val;

}

int cnt;

int main(){

	scanf("%d",&n);

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

	for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y),a[i].id=i;

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

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

		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++){

		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;

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

		fa[fx]=fy;

		ans+=e[i].val;

		p++;

		if(p==n-1)break;

	}

	printf("%lld",ans);

    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.8369968091005298x):
#include<bits/stdc++.h>

#define mp make_pair

#define pb push_back

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef vector<int > vi;

typedef pair<int ,int > pii;

typedef vector<pii> vii;

const int inf=0x3f3f3f3f, maxn=100007, mod=1e9+7;

const ll linf=0x3f3f3f3f3f3f3f3fLL;

const ll P=19260817;

int fa[100007];

int n;

int find(int x){

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

}

struct node{

	int x,y,id;

}a[100007];

bool cmp1(const node& a,const node& b){

	return a.x<b.x;

}

bool cmp2(const node& a,const node& b){

	return a.y<b.y;

}

struct edge{

	int x,y,val;

}e[100005<<2];

bool cmp3(const edge& a,const edge &b){

	return a.val<b.val;

}

int cnt;

int main(){

	scanf("%d",&n);

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

	for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y),a[i].id=i;

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

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

		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++){

		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;

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

		fa[fx]=fy;

		ans+=e[i].val;

		p++;

		if(p==n-1)break;

	}

	printf("%lld",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.4142809165673833x):
#include<bits/stdc++.h>

#define mp make_pair

#define pb push_back

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef vector<int > vi;

typedef pair<int ,int > pii;

typedef vector<pii> vii;

const int inf=0x3f3f3f3f, maxn=100007, mod=1e9+7;

const ll linf=0x3f3f3f3f3f3f3f3fLL;

const ll P=19260817;

int fa[100007];

int n;

inline int find(int x){

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

}

struct node{

	int x,y,id;

}a[100007];

inline bool cmp1(const node& a,const node& b){

	return a.x<b.x;

}

inline bool cmp2(const node& a,const node& b){

	return a.y<b.y;

}

struct edge{

	int x,y,val;

}e[100005<<2];

inline bool cmp3(const edge& a,const edge &b){

	return a.val<b.val;

}

int cnt;

int main(){

	scanf("%d",&n);

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

	for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y),a[i].id=i;

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

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

		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++){

		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;

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

		fa[fx]=fy;

		ans+=e[i].val;

		p++;

		if(p==n-1)break;

	}

	printf("%lld",ans);

    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.8370296139019233x):
#include<bits/stdc++.h>

#define mp make_pair

#define pb push_back

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef vector<int > vi;

typedef pair<int ,int > pii;

typedef vector<pii> vii;

const int inf=0x3f3f3f3f, maxn=100007, mod=1e9+7;

const ll linf=0x3f3f3f3f3f3f3f3fLL;

const ll P=19260817;

int fa[100007];

int n;

inline int find(int x){

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

}

struct node{

	int x,y,id;

}a[100007];

inline bool cmp1(const node& a,const node& b){

	return a.x<b.x;

}

inline bool cmp2(const node& a,const node& b){

	return a.y<b.y;

}

struct edge{

	int x,y,val;

}e[100005<<2];

inline bool cmp3(const edge& a,const edge &b){

	return a.val<b.val;

}

int cnt;

int main(){

	scanf("%d",&n);

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

	for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y),a[i].id=i;

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

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

		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++){

		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;

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

		fa[fx]=fy;

		ans+=e[i].val;

		p++;

		if(p==n-1)break;

	}

	printf("%lld",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.3994126772466202x):
#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;

}