// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



using namespace std;

typedef long long ll;

struct Point

{

    ll x,y;

    int id;

};

struct edge

{

    int a,b,w;

};

bool cmp1(Point a,Point b)

{

    return a.x<b.x;

}

bool cmp2(Point a,Point b)

{

    return a.y<b.y;

}

bool cmp3(edge a,edge b)

{

    return a.w<b.w;

}

int f[100005];

int bcj(int p)

{

    if(f[p]==p)

        return p;

    f[p]=bcj(f[p]);

    return f[p];

}

void join(int p,int q)

{

    int fp=bcj(p);

    int fq=bcj(q);

    if(fp!=fq)

        f[fp]=fq;

}



Point village[100005];

edge e[200005];

int main()

{

    int N;



    scanf("%d",&N);

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

    {

        f[i]=i;

    }

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

    {

        scanf("%lld%lld",&village[i].x,&village[i].y);

        village[i].id=i;

    }



    int cnt=0;

    sort(village,village+N,cmp1);

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

    {

        e[cnt].a=village[i].id;

        e[cnt].b=village[i+1].id;

        e[cnt].w=village[i].x-village[i+1].x;

        if(e[cnt].w<0)

            e[cnt].w*=-1;

        cnt++;

    }

    sort(village,village+N,cmp2);

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

    {

        e[cnt].a=village[i].id;

        e[cnt].b=village[i+1].id;

        e[cnt].w=village[i].y-village[i+1].y;

        if(e[cnt].w<0)

            e[cnt].w*=-1;

        cnt++;

    }



    sort(e,e+cnt,cmp3);

    ll ans=0;

    int p=0;

    while(N-->1)

    {

        while(1)

        {



            if(bcj(e[p].a)!=bcj(e[p].b))

            {

                join(e[p].a,e[p].b);

                ans+=(ll)e[p].w;



                p++;

                break;

            }

            p++;

        }

    }

    printf("%lld",ans);







}


// Fastest known solution for this problem: