// 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];



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;

	}

}



signed main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int n; cin >> n;

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

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

	for (int i=0; i<100001; i++) up[i] = -1;

	long long ans = 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));

			ans += q[i].first;

		}

	}

	cout << ans;

}


// Faster C++ solution for the same problem (measured speedup: 1.3797842660385982x):
#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 ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> 

#define ll long long int

#define ff first

#define ss second

#define pb push_back

#define mp make_pair

#define pll pair<ll,ll>

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

#define sz(x) (ll)x.size() 



ll parent[500005],s[500005];

 

ll Root(ll i)

{

    if(parent[parent[i]]!=parent[i])

        parent[i]=Root(parent[i]);

    return parent[i];

}

void Uni(ll a,ll b)

{

    ll parent_a=Root(a),parent_b=Root(b);

    if(parent_a==parent_b)

        return;

    if(s[parent_a]>=s[parent_b])

    {

         s[parent_a]+=s[parent_b];

         parent[parent_b]=parent_a;

    }

    else

    {

         s[parent_b]+=s[parent_a];

         parent[parent_a]=parent_b;

    }

    return;

} 



int main()

{

    ios_base::sync_with_stdio(false);

    ll n,i,j,k,x,y,t,m;



    cin >> n ;

    vector<pll>v,X,Y;

    vector< pair< ll , pll > >e;



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

    {

        cin >> x >> y;

        v.pb({x,y});

        X.pb({x,i});

        Y.pb({y,i});

    }



    sort(all(X)),sort(all(Y));



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

        s[i]=1,parent[i]=i;





    for(i=0;i<sz(X);i++)

    {

        if(i+1<sz(X))

            e.pb({X[i+1].ff-X[i].ff,{X[i].ss,X[i+1].ss}});

        if(i-1>=0)

            e.pb({X[i].ff-X[i-1].ff,{X[i-1].ss,X[i].ss}});

    }

    for(i=0;i<sz(Y);i++)

    {

        if(i+1<sz(Y))

            e.pb({Y[i+1].ff-Y[i].ff,{Y[i].ss,Y[i+1].ss}});

        if(i-1>=0)

            e.pb({Y[i].ff-Y[i-1].ff,{Y[i-1].ss,Y[i].ss}});

    }



    sort(all(e));

    ll ans=0,tot=0;

    for(i=0;i<sz(e);i++)

    {

        if(tot==n-1)

            break;

        else

        {

            x=e[i].ss.ff,y=e[i].ss.ss;

            if(Root(x)==Root(y))

                continue;

            else

            {

                Uni(x,y);

                ans+=e[i].ff;

                tot++;

            }

        }

    }

    cout<<ans<<endl;

}