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

using namespace std;

#define ll long long

struct p {

	int x, y;

	int indx;

};

const int N = 1e5 + 5;

p points[N];

int n;

int par[N];

int get(int n) {

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

	return par[n] = get(par[n]);

}

vector<int> g[N];

void solve() {

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

		par[i] = i;

	vector<pair<int, pair<int, int> >> edg;

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

//		g[i].clear();

//		g[i].resize(n);

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

//			g[i][j] = j;

			int v = g[i][j];

			ll c = min(abs(points[i].x - points[v].x),

					abs(points[i].y - points[v].y));

			edg.push_back(make_pair(c, make_pair(i, v)));

		}

	}

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

	ll ans = 0;

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

		int u = edg[i].second.first;

		int v = edg[i].second.second;

		int r1 = get(u);

		int r2 = get(v);

		if (r1 == r2) continue;

		//cout << u << "  " << v << endl;

		ans += edg[i].first;

		par[r1] = r2;

	}

	cout << ans << endl;

}

void gen(vector<pair<int, int> > &a, vector<pair<int, int> > &b) {



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



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



		auto it = lower_bound(b.begin(), b.end(), make_pair(a[i].first, -1));

		if (it != b.end()) {

			g[a[i].second].push_back(it->second);

			g[it->second].push_back(a[i].second);

		}

		if (it != b.begin()) {

			it--;



			g[a[i].second].push_back(it->second);

			g[it->second].push_back(a[i].second);

		}

	}

}

int main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cin >> n;

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

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

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

		points[i].indx = i;

		v.push_back(make_pair(points[i].x, make_pair(points[i].y, i)));

	}

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

	vector<pair<int, int> > last, cur;

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

		int X = v[i].first;

		while (i < n && v[i].first == X) {

			cur.push_back(v[i].second);

			i++;

		}

		if (last.size()) gen(last, cur);

		last = cur;

		cur.clear();

	}

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

		int u = v[i].second.second;

		int V = v[i + 1].second.second;

		g[u].push_back(V);

		g[V].push_back(u);

	}

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

		swap(v[i].first, v[i].second.first);

	last.clear();

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

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

		int X = v[i].first;



		while (i < n && v[i].first == X) {

			cur.push_back(v[i].second);

			i++;

		}

		if (last.size()) gen(last, cur);

		last = cur;

		cur.clear();

	}

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

		int u = v[i].second.second;

		int V = v[i + 1].second.second;

		g[u].push_back(V);

		g[V].push_back(u);

	}



	solve();

	return 0;

}


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

using namespace std;



#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);

#define sfi(x) scanf("%d" , &x)

#define sfi2(x, y) scanf("%d%d" , &x,&y)

#define sfl2(x, y) scanf("%I64d%I64d" , &x,&y)

#define sfl(x) scanf("%I64d" , &x)

#define sfl3(x, y, z) scanf("%I64d%I64d%I64d", &x, &y, &z)

#define sfd(x) scanf("%lf", &x)

#define sfd2(x, y) scanf("%lf", &x, &y)

#define sfs(x) scanf("%s", &x)

#define testv(x) cerr << x << ' '

#define wtf(s) freopen((s), "w", stdout)

#define rff(s) freopen((s), "r", stdin)

#define test cerr << "here"

#define fst(x) cout << fixed << setprecision(x)

#define mod 1000000007

#define pi (2 * acos(0))

typedef long double ld;

typedef long long ll;



int par[200005], rnk[200005];



int findp(int x)

{

    if (par[x] == x)

        return x;

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

}



bool iscon(int x, int y)

{

    if (findp(x) == findp(y))

        return true;

    return false;

}



void con(int x, int y)

{

    int px = findp(x), py = findp(y);

    if (rnk[px] > rnk[py])

        par[py] = px;

    else if (rnk[px] < rnk[py])

        par[px] = py;

    else

        par[px] = py, rnk[px]++;

}





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

int main()

{

    ENGZ

    int n;

    ll ans = 0;

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

        par[i] = i;

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

    map<ll, vector<pair<int, int>>> mp;

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

    {

        mp[abs(a[i].first.first - a[i + 1].first.first)].push_back({a[i].second, a[i + 1].second});

        mp[abs(b[i].first.first - b[i + 1].first.first)].push_back({b[i].second, b[i + 1].second});

    }

    for (auto it: mp)

    {

        for (auto i: it.second)

        {

            if (!iscon(i.first, i.second))

                con(i.first, i.second), ans += it.first;

        }

    }

    cout << ans;

    return 0;

}