// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.3978371508301064x):
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#define MuhammedAly ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

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

#define allr(x) (x).rbegin(),(x).rend()

#define pb push_back

#define eb emplace_back

#define popc(x) __builtin_popcount(x)

#define LLMX 1e18

#define NotinRange(n,m) i<0||i>=(n)||j<0||j>=(m)

#define mem(x,y) memset((x),(y),sizeof (x));

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

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

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

#define biEdge(v, a, b) (v)[(a)].pb((b)), (v)[(b)].pb((a))

#define pi (2 * acos(0))

#define X real()

#define Y imag()

#define cross(a, b) ((conj(a) * (b)).Y)

#define dot(a, b) ((conj(a) * (b)).X)

#define vec(a,b) ((b) - (a))

#define ll long long

typedef long double ld;

typedef complex<double>point;

typedef tuple<int, int, int> line;

typedef pair<point, point>segment;

typedef vector<point>polygon;

const int N = 1e5 + 5, M = 100 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7, base = 131, mod2 = 1000136437, mod3 = 998244353;

const double EPS = 1e-9;

int n, rp[N], si[N];

ll ans;

vector<pair<int, pair<int, int>>>x, y;

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

int find(int u)

{

	return (rp[u] == u)? u : rp[u] = find(rp[u]);

}

bool join(int u, int v)

{

	u = find(u), v = find(v);

	if(u == v)

		return false;

	if(si[u] < si[v])

		swap(u, v);

	si[u] += si[v];

	rp[v] = u;

	return true;

}

int main()

{

    MuhammedAly

    cin >> n;

    fill(si, si + n + 1, 1);

    iota(rp, rp + n + 1, 0);

    x.resize(n);

    y.resize(n);

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

    {

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

    	y[i].first = x[i].second.first;

    	y[i].second.first = x[i].first;

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

    }

    sort(all(x));

    sort(all(y));

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

    {

    	mp[x[i + 1].first - x[i].first].pb({x[i].second.second, x[i + 1].second.second});

    	mp[y[i + 1].first - y[i].first].pb({y[i].second.second, y[i + 1].second.second});

    }

    for(auto i : mp)

    {

    	for(auto j : i.second)

    		if(join(j.second, j.first))

    			ans += i.first;

    }

    cout << ans;

    return 0;

}