stablegradients/pie-gem5-pairs huggingface dataset:
// 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: 4.970794306392168x):
#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";

}
// 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: 2.5453237022347905x):
#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;

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

using namespace std;

const int N = 1e5 + 5;

int n, par[N], sz[N];

struct  poi{

    int x, y, idx;

}   p[N];

struct  edge    {

    int x, y, w;

}   e;

vector<edge>    E, MST;

int ans = 0;

bool cmp1(poi A,poi B)  {

    return A.x < B.x;

}

bool cmp2(poi A,poi B)  {

    return A.y < B.y;

}

bool cmp(edge e1,edge e2)   {

    return  e1.w < e2.w;

}

int findSet(int x)  {

    return  par[x] == x ? x : par[x] = findSet(par[x]);

}

void unionSet(int x,int y) {

    int a = findSet(x);

    int b = findSet(y);

    if(a == b)  return;

    if(sz[a] < sz[b])   swap(a,b);

    par[b] = a; sz[a] += sz[b];

}

int main()  {

    cin >> n;

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

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

        p[i].idx = i;

        par[i] = i;

        sz[i] = 1;

    }

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

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

        e.x = p[i].idx;

        e.y = p[i + 1].idx;

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

        E.push_back(e);

    }

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

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

        e.x = p[i].idx;

        e.y = p[i + 1].idx;

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

        E.push_back(e);

    }

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

    int i = 0;

    while(MST.size() < n - 1)   {

        int x = E[i].x, y = E[i].y, W = E[i].w;

        ++i;

        if(findSet(x) == findSet(y))    continue;

        ans += W;   MST.push_back(E[i - 1]);

        unionSet(x,y);

    }

    cout << ans << endl;

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

using namespace std;



int p[100001];



struct node{

	int x, y, id;

}nodes[100001];



struct edge{

	int u, v, cost;

}mst[100001];



int find(int u){

	if(p[u] == u)

		return p[u];

	else 

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

}



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

	return a.x < b.x;

}



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

	return a.y < b.y;

}



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

	return a.cost < b.cost;

}



signed main(){

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

	

	int n, k = 0;

	cin >> n;

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

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

		nodes[i].id = i;

	}

	

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

	

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

		mst[k].u = nodes[i].id;

		mst[k].v = nodes[i+1].id;

		mst[k].cost = abs(nodes[i].x - nodes[i+1].x);

		k++;

	}

	

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

	

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

		mst[k].u = nodes[i].id;

		mst[k].v = nodes[i+1].id;

		mst[k].cost = abs(nodes[i].y - nodes[i+1].y);

		k++;

	}

	

	sort(mst, mst + k, cmp3);

	

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

		p[i]=i;

		

	long long ans=0;

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

		int a = mst[i].u, b = mst[i].v;

		a = find(a), b = find(b);

		if(a != b) {

			ans += mst[i].cost;

			p[a] = b;

		}

	}

	

	cout << ans;

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

using namespace std;



int p[100001];



struct node{

	int x, y, id;

}nodes[100001];



struct edge{

	int u, v, cost;

}mst[100001];



int find(int u){

	if(p[u] == u)

		return p[u];

	else 

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

}



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

	return a.x < b.x;

}



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

	return a.y < b.y;

}



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

	return a.cost < b.cost;

}



signed main(){

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

	

	int n, k = 0;

	cin >> n;

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

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

		nodes[i].id = i;

	}

	

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

	

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

		mst[k].u = nodes[i].id;

		mst[k].v = nodes[i+1].id;

		mst[k].cost = abs(nodes[i].x - nodes[i+1].x);

		k++;

	}

	

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

	

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

		mst[k].u = nodes[i].id;

		mst[k].v = nodes[i+1].id;

		mst[k].cost = abs(nodes[i].y - nodes[i+1].y);

		k++;

	}

	

	sort(mst, mst + k, cmp3);

	

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

		p[i]=i;

		

	long long ans=0;

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

		int a = mst[i].u, b = mst[i].v;

		a = find(a), b = find(b);

		if(a != b) {

			ans += mst[i].cost;

			p[a] = b;

		}

	}

	

	cout << ans;

}
// 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.5505145331997143x):
#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<iostream>

#include<vector>

#include<queue>

#include<stack>

#include<map>

#include<set>

using namespace std;

typedef long long ll;

const ll inf=1e18;

const int N=1e5+3;

struct oy{

	int x,y,id;

}a[N],b[N];

struct dio{

	int u,v,w;

}e[N<<1];

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

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

bool cmp3(dio a,dio b){return a.w<b.w;}

int pre[N],n,cnt;

int find(int x){

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

}

int main(){

    cin>>n;

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

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

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

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

    	b[i].x=a[i].x;b[i].y=a[i].y;

    }

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

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

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

    	e[cnt].u=a[i-1].id;

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

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

    	cnt++;

    }

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

    	e[cnt].u=b[i-1].id;

    	e[cnt].v=b[i].id;

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

    	cnt++;

    }

    int num=0;

    ll ans=0;

    sort(e,e+cnt,cmp3);

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

    	int x=find(e[i].u),y=find(e[i].v);

    	if(x!=y){

    		pre[x]=y;

    		ans+=e[i].w;

    		num++;

    	}

    	if(num==n-1) break;

    }

    cout<<ans<<endl;

    return 0;

}
// 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.3949166976610354x):
#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<iostream>

#include<vector>

#include<queue>

#include<stack>

#include<map>

#include<set>

using namespace std;

typedef long long ll;

const ll inf=1e18;

const int N=1e5+3;

struct oy{

	int x,y,id;

}a[N],b[N];

struct dio{

	int u,v,w;

}e[N<<1];

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

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

bool cmp3(dio a,dio b){return a.w<b.w;}

int pre[N],n,cnt;

int find(int x){

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

}

int main(){

    cin>>n;

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

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

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

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

    	b[i].x=a[i].x;b[i].y=a[i].y;

    }

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

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

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

    	e[cnt].u=a[i-1].id;

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

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

    	cnt++;

    }

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

    	e[cnt].u=b[i-1].id;

    	e[cnt].v=b[i].id;

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

    	cnt++;

    }

    int num=0;

    ll ans=0;

    sort(e,e+cnt,cmp3);

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

    	int x=find(e[i].u),y=find(e[i].v);

    	if(x!=y){

    		pre[x]=y;

    		ans+=e[i].w;

    		num++;

    	}

    	if(num==n-1) break;

    }

    cout<<ans<<endl;

    return 0;

}
// 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.5627542583413603x):
#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;

}