// 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.3823464429580978x):
#include <iostream>

#include <algorithm>

#include <cstdio>

#define rush() int T;cin>>T;while(T--)

#define go(a) while(cin>>a)

#define ms(a,b) memset(a,b,sizeof a)

#define E 1e-8

using namespace std;

typedef long long ll;

const int N=1e5+5;



    int n,m,t;

    int fa[N];

    int i,j,k;

struct point

{

    int x,y,id;

}p[N];

struct Edge

{

    int u,v,valu;

    bool const operator<(const Edge &b)

    const{

        return valu<b.valu;

    }

}edge[N];

int Find(int son)

{

    return son==fa[son] ? son : fa[son]=Find(fa[son]);

}

bool Union(int x,int y)

{

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

    if(x==y) return 1;

    else{

        fa[y]=x;

        return 0;

    }

}

bool cmpx(point &a,point &b){

    return a.x<b.x;

}

bool cmpy(point &a,point &b){

    return a.y<b.y;

}

int main()

{

    cin.tie(0);istream::sync_with_stdio(false);

    while(cin>>n){

        for(i=1;i<=n;i++) fa[i]=i;



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

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

            p[i].id=i;

        }



        int num=0;

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

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

            edge[++num].u=p[i].id;

            edge[num].v=p[i+1].id;

            edge[num].valu=min(abs(p[i].x-p[i+1].x),abs(p[i].y-p[i+1].y));

        }

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

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

            edge[++num].u=p[i].id;

            edge[num].v=p[i+1].id;

            edge[num].valu=min(abs(p[i].x-p[i+1].x),abs(p[i].y-p[i+1].y));

        }



        stable_sort(edge+1,edge+1+num);

        int ans=0,cnt=0;

        for(i=1;i<=num;i++){

            if(0==Union(edge[i].u,edge[i].v)){

                ans+=edge[i].valu;

                cnt++;

            }

            if(cnt==n-1) break;

        }

        cout<<ans<<endl;

    }

}