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

using namespace std;

#define MAX 300000

#define int long long

struct Pair{

	int x,y, id;

};

int n;

Pair a[MAX+1];

typedef Pair PP;

typedef pair<int,int> pp;

vector<pair<int,int> > G[MAX+1];

bool cmp(PP a,PP b){return a.x<b.x;};

bool CMP(PP a,PP b){return a.y<b.y;};

bool visited[MAX+1];

int ans;

#define fi first

#define se second

void DJK(int x){

	ans = 0;

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

	pq.push({0,x});

	while(!pq.empty()){

		int u = pq.top().se;

		int w = pq.top().fi;

		pq.pop();

		if(visited[u]) continue;

		ans+=w;

		visited[u] = 1;

		for(pp ii : G[u]){

			int v = ii.se;

			pq.push({ii.fi,v});

		}

	}

}

signed main(){

	cin >> n;

	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=2;i<=n;i++){

		G[a[i].id].push_back({a[i].x-a[i-1].x,a[i-1].id});

		G[a[i-1].id].push_back({a[i].x-a[i-1].x,a[i].id});

	}

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

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

		G[a[i].id].push_back({a[i].y-a[i-1].y,a[i-1].id});

		G[a[i-1].id].push_back({a[i].y-a[i-1].y,a[i].id});

	}

	DJK(1);

	cout << ans;

}

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

using namespace std;

//#define cerr if (false) cerr

#define db(x) cerr << #x << "=" << x << endl

#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl

#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl

#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl

#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl

template <typename A, typename B>

ostream& operator<<(ostream& os, const pair<A, B>& x) {

	return os << "(" << x.first << "," << x.second << ")";

}

typedef long long ll;

typedef long double ld;

int main() {

	int n;

	scanf("%d", &n);

	vector<int> X(n), Y(n), id(n);

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

		scanf("%d%d", &X[i], &Y[i]);

		id[i] = i;

	}

	vector<pair<int, pair<int, int>>> edges;

	sort(id.begin(), id.end(), [&](int a, int b) {

		return X[a] < X[b];

	});

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

		edges.push_back({min(abs(X[id[i]] - X[id[i + 1]]), abs(Y[id[i]] - Y[id[i + 1]])), {id[i], id[i + 1]}});

	}

	sort(id.begin(), id.end(), [&](int a, int b) {

		return Y[a] < Y[b];

	});

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

		edges.push_back({min(abs(X[id[i]] - X[id[i + 1]]), abs(Y[id[i]] - Y[id[i + 1]])), {id[i], id[i + 1]}});

	}

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

	vector<int> par(n);

	iota(par.begin(), par.end(), 0);

	function<int(int)> getRoot = [&](int a) {

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

		return par[a] = getRoot(par[a]);

	};

	ll ans = 0;

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

		int a, b;

		tie(a, b) = edges[i].second;

		int pa = getRoot(a), pb = getRoot(b);

		if (pa == pb) continue;

		par[pa] = pb;

		ans += edges[i].first;

	}

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

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

#include<fstream>

#include<sstream>

#include<algorithm>

#include<cstdio>

#include<cassert>

#include<cmath>

#include<cstdlib>

#include<cstring>

#include<string>

#include<deque>

#include<queue>

#include<vector>

#include<list>

#include<map>

#include<set>

#include<bitset>

#include<stack>

#include<iomanip>

#include<utility>

#include<functional>

using namespace std;

int n,m;

int ans;

int par[100005];

vector<int> edge[100005];

vector<pair<int,int> > el;

pair<int,int> e[200005];

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

int find(int x)

{

	if(par[x]!=x)

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

	else

		return par[x];

}

bool unite(int x,int y)

{

	x=find(x);

	y=find(y);

	if(x==y)

		return false;

	else

	{

		par[x]=y;

		return true;

	}

}

int main()

{

	cin>>n;

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

	{

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

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

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

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

	}

	sort(a,a+n);

	sort(b,b+n);

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

	{

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

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

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

	}

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

	{

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

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

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

	}

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

		par[i]=i;

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

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

	{

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

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

		if(unite(xx,yy))

			ans+=el[i].first;

	}

	cout<<ans;

	return 0;

}


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

using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int, pii> pipii;

typedef pair<pii, pii> piipii;



#define mp make_pair

#define fi first

#define se second

#define all(a) (a).begin(), (a).end()

#define sz(a) (int)(a).size()

#define eb emplace_back

pii p[100005];

int x[100005], y[100005];

int r[100005];

int root(int a){

    return r[a] == a?a:(r[a]=root(r[a]));

}

void merge(int a, int b){

    a = root(a), b = root(b);

    if(a != b) r[a] = b;

}

int main(){

    int n;

    scanf("%d", &n);   

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

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

        p[i] = pii(x[i], i);

        r[i] = i;

    }

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

    vector<pipii> edges;

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    for(int i=1;i<=n;i++) p[i] = pii(y[i], i);

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

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    int ans = 0;

    for(int i=0;i<sz(edges);i++){

        if(root(edges[i].se.fi) != root(edges[i].se.se)){

            merge(edges[i].se.fi, edges[i].se.se);

            ans += edges[i].fi;

        }

    }

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

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

using namespace std;



#define fi first

#define se second

const int inf = 1e9 + 7;

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

pair <pair <int, int>, int> a[100010]; bool dis[100010];

priority_queue<pair <int, pair <int, int> >, vector<pair <int, pair <int, int> > >, greater<pair <int, pair <int, int> > > > wl1, wl2;

int find(int a) {

	return (par[a] < 0 ? a : par[a] = find(par[a]));

}

void unionset(int a, int b) {

	int c = find(a), d = find(b);

	if(par[c] < par[d]) par[c] += par[d], par[d] = c;

	else par[d] += par[c], par[c] = d;

}

int main() {

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

	scanf("%d", &n);

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

		scanf("%d%d", &a[i].fi.fi, &a[i].fi.se);

		a[i].se = i;

	}

	sort(a, a + n);

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

		wl1.push({a[i+1].fi.fi - a[i].fi.fi, {a[i].se, a[i+1].se}});

		swap(a[i].fi.fi, a[i].fi.se);

	}

	swap(a[n-1].fi.fi, a[n-1].fi.se);

	sort(a, a + n);

	for(int i = 0; i < n-1; i++) wl2.push({a[i+1].fi.fi - a[i].fi.fi, {a[i].se, a[i+1].se}});

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

		while(find(wl1.top().se.fi) == find(wl1.top().se.se)) wl1.pop();

		while(find(wl2.top().se.fi) == find(wl2.top().se.se)) wl2.pop();

		int d1 = inf, d2 = inf, u1, u2, v1, v2;

		if(not wl1.empty()) d1 = wl1.top().fi, u1 = wl1.top().se.fi, v1 = wl1.top().se.se;

		if(not wl2.empty()) d2 = wl2.top().fi, u2 = wl2.top().se.fi, v2 = wl2.top().se.se;

		if(d1 < d2) {

			unionset(u1, v1);

			ans += d1;

		}

		else {

			unionset(u2, v2);

			ans += d2;

		}

	}

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

}

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

using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int, pii> pipii;

typedef pair<pii, pii> piipii;



#define mp make_pair

#define fi first

#define se second

#define all(a) (a).begin(), (a).end()

#define sz(a) (int)(a).size()

#define eb emplace_back

pii p[100005];

int x[100005], y[100005];

int r[100005];

int root(int a){

    return r[a] == a?a:(r[a]=root(r[a]));

}

void merge(int a, int b){

    a = root(a), b = root(b);

    if(a != b) r[a] = b;

}

int main(){

    int n;

    scanf("%d", &n);   

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

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

        p[i] = pii(x[i], i);

        r[i] = i;

    }

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

    vector<pipii> edges;

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    for(int i=1;i<=n;i++) p[i] = pii(y[i], i);

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

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    int ans = 0;

    for(int i=0;i<sz(edges);i++){

        if(root(edges[i].se.fi) != root(edges[i].se.se)){

            merge(edges[i].se.fi, edges[i].se.se);

            ans += edges[i].fi;

        }

    }

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

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



using namespace std;



const int N = 2e5 + 5;



struct point {

    int x, y, id;

} a[N];



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

    if (a.x != b.x) {

        return a.x < b.x;

    }

    return a.y < b.y;

}



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

    if (a.y != b.y) {

        return a.y < b.y;

    }

    return a.x < b.x;

}



int n;

bool visited[N];

vector<pair<long long, int> > g[N];



long long prim(int x) {

    priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > pq;

    long long ans = 0;

    pq.push({0, x});

    while(!pq.empty()) {

        pair<long long, int> p = pq.top();

        pq.pop();

        x = p.second;

        if (visited[x]) {

        	continue;

		}

        ans += p.first;

        visited[x] = true;

        for(int i = 0; i < g[x].size(); i++) {

            int y = g[x][i].second;

            if (!visited[y]) {

            	pq.push(g[x][i]);

            }

        }

    }

    return ans;

}



int main() {

	ios::sync_with_stdio(false);

	cin.tie(nullptr);

	cin >> n;

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

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

		a[i].id = i + 1;

	}

	sort(a, a + n, cmp1);

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

		g[a[i].id].push_back({abs(a[i].x - a[i - 1].x), a[i - 1].id});

		g[a[i - 1].id].push_back({abs(a[i].x - a[i - 1].x), a[i].id});

	}

	sort(a, a + n, cmp2);

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

		g[a[i].id].push_back({abs(a[i].y - a[i - 1].y), a[i - 1].id});

		g[a[i - 1].id].push_back({abs(a[i].y - a[i - 1].y), a[i].id});

	}

	cout << prim(1) << '\n';

	return 0;

}


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

using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int, pii> pipii;

typedef pair<pii, pii> piipii;



#define mp make_pair

#define fi first

#define se second

#define all(a) (a).begin(), (a).end()

#define sz(a) (int)(a).size()

#define eb emplace_back

pii p[100005];

int x[100005], y[100005];

int r[100005];

int root(int a){

    return r[a] == a?a:(r[a]=root(r[a]));

}

void merge(int a, int b){

    a = root(a), b = root(b);

    if(a != b) r[a] = b;

}

int main(){

    int n;

    scanf("%d", &n);   

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

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

        p[i] = pii(x[i], i);

        r[i] = i;

    }

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

    vector<pipii> edges;

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    for(int i=1;i<=n;i++) p[i] = pii(y[i], i);

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

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    int ans = 0;

    for(int i=0;i<sz(edges);i++){

        if(root(edges[i].se.fi) != root(edges[i].se.se)){

            merge(edges[i].se.fi, edges[i].se.se);

            ans += edges[i].fi;

        }

    }

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

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



using namespace std;

using ll = long long;



const int N = 2e5 + 5;



struct point {

    int x, y, id;

} a[N];



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

    if (a.x != b.x) {

        return a.x < b.x;

    }

    return a.y < b.y;

}



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

    if (a.y != b.y) {

        return a.y < b.y;

    }

    return a.x < b.x;

}



int n;

bool visited[N];

vector<pair<ll, int> > g[N];



ll prim(int x) {

    priority_queue<pair<ll, int>, vector<pair<ll, int> >, greater<pair<ll, int> > > pq;

    ll ans = 0;

    pq.push({0, x});

    while(!pq.empty()) {

        pair<ll, int> p = pq.top();

        pq.pop();

        x = p.second;

        if (visited[x]) {

        	continue;

		}

        ans += p.first;

        visited[x] = true;

        for(int i = 0; i < g[x].size(); i++) {

            int y = g[x][i].second;

            if (!visited[y]) {

            	pq.push(g[x][i]);

            }

        }

    }

    return ans;

}



int main() {

	ios::sync_with_stdio(false);

	cin.tie(nullptr);

	cin >> n;

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

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

		a[i].id = i + 1;

	}

	sort(a, a + n, cmp1);

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

		g[a[i].id].push_back({abs(a[i].x - a[i - 1].x), a[i - 1].id});

		g[a[i - 1].id].push_back({abs(a[i].x - a[i - 1].x), a[i].id});

	}

	sort(a, a + n, cmp2);

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

		g[a[i].id].push_back({abs(a[i].y - a[i - 1].y), a[i - 1].id});

		g[a[i - 1].id].push_back({abs(a[i].y - a[i - 1].y), a[i].id});

	}

	cout << prim(1) << '\n';

	return 0;

}


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

using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int, pii> pipii;

typedef pair<pii, pii> piipii;



#define mp make_pair

#define fi first

#define se second

#define all(a) (a).begin(), (a).end()

#define sz(a) (int)(a).size()

#define eb emplace_back

pii p[100005];

int x[100005], y[100005];

int r[100005];

int root(int a){

    return r[a] == a?a:(r[a]=root(r[a]));

}

void merge(int a, int b){

    a = root(a), b = root(b);

    if(a != b) r[a] = b;

}

int main(){

    int n;

    scanf("%d", &n);   

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

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

        p[i] = pii(x[i], i);

        r[i] = i;

    }

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

    vector<pipii> edges;

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    for(int i=1;i<=n;i++) p[i] = pii(y[i], i);

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

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    int ans = 0;

    for(int i=0;i<sz(edges);i++){

        if(root(edges[i].se.fi) != root(edges[i].se.se)){

            merge(edges[i].se.fi, edges[i].se.se);

            ans += edges[i].fi;

        }

    }

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

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

using namespace std;

#define MAX 300000

#define int long long

struct Pair{

	int x,y, id;

};

int n;

Pair a[MAX+1];

typedef Pair PP;

typedef pair<int,int> pp;

vector<pair<int,int> > G[MAX+1];

bool cmp(PP a,PP b){return a.x<b.x;};

bool CMP(PP a,PP b){return a.y<b.y;};

bool visited[MAX+1];

int ans;

#define fi first

#define se second

void DJK(int x){

	ans = 0;

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

	pq.push({0,x});

	while(!pq.empty()){

		int u = pq.top().se;

		int w = pq.top().fi;

		pq.pop();

		if(visited[u]) continue;

		ans+=w;

		visited[u] = 1;

		for(pp ii : G[u]){

			int v = ii.se;

			pq.push({ii.fi,v});

		}

	}

}

signed main(){

	cin >> n;

	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=2;i<=n;i++){

		G[a[i].id].push_back({a[i].x-a[i-1].x,a[i-1].id});

		G[a[i-1].id].push_back({a[i].x-a[i-1].x,a[i].id});

	}

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

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

		G[a[i].id].push_back({a[i].y-a[i-1].y,a[i-1].id});

		G[a[i-1].id].push_back({a[i].y-a[i-1].y,a[i].id});

	}

	DJK(1);

	cout << ans;

}

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

using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int, pii> pipii;

typedef pair<pii, pii> piipii;



#define mp make_pair

#define fi first

#define se second

#define all(a) (a).begin(), (a).end()

#define sz(a) (int)(a).size()

#define eb emplace_back

pii p[100005];

int x[100005], y[100005];

int r[100005];

int root(int a){

    return r[a] == a?a:(r[a]=root(r[a]));

}

void merge(int a, int b){

    a = root(a), b = root(b);

    if(a != b) r[a] = b;

}

int main(){

    int n;

    scanf("%d", &n);   

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

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

        p[i] = pii(x[i], i);

        r[i] = i;

    }

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

    vector<pipii> edges;

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    for(int i=1;i<=n;i++) p[i] = pii(y[i], i);

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

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    int ans = 0;

    for(int i=0;i<sz(edges);i++){

        if(root(edges[i].se.fi) != root(edges[i].se.se)){

            merge(edges[i].se.fi, edges[i].se.se);

            ans += edges[i].fi;

        }

    }

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

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

#define int long long

using namespace std;

typedef pair<int, pair<int,int> > iii;

int n,cnt=0,res=0,x,y,farm[300000];

vector< pair<int,int> > adjx,adjy;

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

int Find(int n)

{

	if(farm[n]<0) return n;

	else return farm[n]=Find(farm[n]);

}

void Union(int x,int y)

{

	x=Find(x),y=Find(y);

	if(x!=y)

	{

		farm[x]+=farm[y];

		farm[y]=x;

	}

}

signed main()

{

	cin>>n;

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

	{

		cin>>x>>y;

		adjx.push_back({x,i});

		adjy.push_back({y,i});

	}

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

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

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

	{

		pq.push({abs(adjx[i].first-adjx[i+1].first),{adjx[i].second,adjx[i+1].second}});

		pq.push({abs(adjy[i].first-adjy[i+1].first),{adjy[i].second,adjy[i+1].second}});

	}

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

	while(!pq.empty())

	{

		if(cnt==n-1) break;

		int u=pq.top().second.first,v=pq.top().second.second,val=pq.top().first;

		pq.pop();

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

		{

			cnt++;

			Union(u,v);

			res=res+val;

		}

	}

	cout<<res;

}

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



using namespace std;

const int N = 1e5 + 5;

int p[N] , w[N];

int n;

int f(int u){

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

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

}

void connect(int a , int b){

    a = f(a);

    b = f(b);

    if(a == b)return;

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

    w[a] += w[b];

    p[b] = a;

}



void init(){

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

        p[i] = i , w[i] = 1;

}

int main()

{

    ios_base::sync_with_stdio(0);

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

    cin>>n;

    init();

    vector< pair<int,int> >vx,vy;

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

    {

        int x,y;

        cin>>x>>y;

        vx.push_back({x,i});

        vy.push_back({y,i});

    }

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

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

    vector<pair< int , pair<int,int> > >v;

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

        v.push_back({vx[i].first-vx[i-1].first,{vx[i].second,vx[i-1].second}});



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

        v.push_back({vy[i].first-vy[i-1].first,{vy[i].second,vy[i-1].second}});

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

    long long ans=0;

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

    {

        int a=v[i].second.first,b=v[i].second.second;

        if(f(a)==f(b)) continue;

        connect(a,b);

        ans+=v[i].first;

    }

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



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

#define int long long

using namespace std;



int p[100010];

int n, k = 0;



struct node

{

	int x,y,id;

} nodes[100010];



struct edge

{

	int u,v,cost;

} mst[100010];



int Union(int u)

{

	if (p[u] == u) {return p[u];}

    return p[u] = Union(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::sync_with_stdio(0);

    cin.tie(0); cout.tie(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;

	}

	int ans = 0;

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

	{

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

		a=Union(a);

		b=Union(b);

		if(a!=b)

		{

			ans+=mst[i].cost;

			p[a]=b;

		}

	}

	cout << ans;

	return 0;

}