#include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
const ll MOD = (ll)1e9+7;
const ll INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

ll modpow(ll x, ll b){
	ll res = 1;
	while(b){
		if(b&1)res = res * x % MOD;
		x = x * x % MOD;
		b>>=1;
	}
	return res;
}

ll modinv(ll x){
	return modpow(x, MOD-2);
}

int main(){
	int n;
	cin >> n;
	vector<vector<ll>> citys(n, vector<ll>(2));
	foreach(i, citys){ 
		foreach(j, i)cin>>j;
	}


	vector<vector<pll>> roads(n);

	rep(i, 2){
		vector<pll> line;
		rep(j, n){
			line.emplace_back(citys[j][i], j);
		}
		sort(all(line));
		rep(j, n-1){
			pll a = line[j];
			pll b = line[j+1];
			roads[a.second].emplace_back(abs(a.first-b.first), b.second);
			roads[b.second].emplace_back(abs(a.first-b.first), a.second);
		}
	}

	vector<bool> used(n, false);
	priority_queue<pll, vector<pll>, greater<pll>> pq;
	ll ans = 0;
	pq.emplace(0, 0);
	while(pq.size()){
		pll d = pq.top();
		pq.pop();
		if(used[d.second])continue;
		used[d.second] = true;
		ans += d.first;
		foreach(i, roads[d.second]){
			if(used[i.second])continue;
			pq.push(i);
		}
	}

	cout << ans << endl;

	return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
using namespace std;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i,n) for(long long i=0;i<n;i++)
#define rp(i,n) for(long long i=1;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define V vector
long long p = 1000000007;
vel rui(19, 1);
vel par;
int root(int ser) {
	if (par[ser] == -1) { return ser; }
	int ans = root(par[ser]);
	par[ser] = ans;
	return ans;
}
bool marge(pin a) {
	int x = root(a.first);
	int y = root(a.second);
	if (x != y) {par[x] = y;}
	return x != y;
}
signed main() {
	int n; cin >> n;
	vel x(n), y(n);
	V<pair<pin, int>> xy(n);
	V<pair<pin, int>> yx(n);
	rep(i, n) { 
		cin >> x[i] >> y[i]; 
		xy[i] = mkp(mkp(x[i], y[i]), i);
		yx[i] = mkp(mkp(y[i], x[i]), i);
	}
	sor(xy); sor(yx);
	V<pair<int, pin>> way(0);
	rep(i, n - 1) {
		int diff = xy[i + 1].first.first - xy[i].first.first;
		pin path = mkp(xy[i].second, xy[i+1].second);
		way.push_back(mkp(diff, path));
	}
	rep(i, n - 1) {
		int diff = yx[i + 1].first.first - yx[i].first.first;
		pin path = mkp(yx[i].second, yx[i + 1].second);
		way.push_back(mkp(diff, path));
	}
	sor(way);
	int ans = 0;
	par = vel(n, -1);
	rep(i, way.size()) {
		if (marge(way[i].second)) { ans += way[i].first; }
	}
	cout << ans << endl;
	return 0;
}
