// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

#include<cmath>

#include<iostream>

using namespace std;

const int inf=0x3f3f3f3f;

const int maxn=1e5+10;



struct edge{

    int u,v,w;

    edge(int _u=0,int _v=0,int _w=0):u(_u),v(_v),w(_w){

    }

    bool operator<(const edge & a)const{

        return w<a.w;

    }

}es[2*maxn];



struct point{

    int x,y,id;

    point(int _x=0,int _y=0,int _id=0):x(_x),y(_y),id(_id){}

} ps[maxn];



int n,cnt,f[maxn];



bool cmpx(point a,point b){

    return a.x<b.x;

}



bool cmpy(point a,point b){

    return a.y<b.y;

}



int findf(int a){

    return ((f[a]==-1)?a:f[a]=findf(f[a]));

}



int kruskal(){

    int i,ans=0,cc=0;

    memset(f,0xff,sizeof(f));

    sort(es,es+cnt);

    for(i=0;i<cnt;i++){

        int u=es[i].u,v=es[i].v,w=es[i].w;

        int fu=findf(u),fv=findf(v);

        if(fu!=fv){

            ans+=w,++cc;

            f[fu]=fv;

        }

        if(cc==n-1)break;

    }

    if(cc<n-1)return -1;

    return ans;

}

int main(){

    int i,j,k,c,un,vn;

    scanf("%d",&n);

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

        scanf("%d%d",&ps[i].x,&ps[i].y);

        ps[i].id=i;

    }

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

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

        es[cnt++]=edge(ps[i].id,ps[i+1].id,(ps[i+1].x-ps[i].x));

    }

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

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

        es[cnt++]=edge(ps[i].id,ps[i+1].id,(ps[i+1].y-ps[i].y));

    }

    printf("%d\n",kruskal());

    return 0;

}


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



using namespace std;



struct INFO {

	int zhou,ord;

	bool operator < (const INFO &b) const {

		return zhou < b.zhou;

	}

}X[100010],Y[100010];

int N,fa[100010];



int fin (int u) {

	if(fa[u] == u)

		return u;

	return fa[u] = fin(fa[u]);

}



struct edge {

	int u,v,w;

	bool operator < (const edge &b) const {

		return w < b.w;

	}

}e[200010];



int main () {

	scanf("%d",&N);



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

		scanf("%d%d",&X[i].zhou,&Y[i].zhou);

		X[i].ord = i;

		Y[i].ord = i;

	}

	sort(X + 1,X + 1 + N);

	sort(Y + 1,Y + 1 + N);



	int l = 0,r = 1000000000;

	int mid,cnt;

	while(l <= r) {

		mid = (l + r) >> 1;

		cnt = 0;

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

			fa[i] = i;



		for(int i = 2;i <= N;++i) {

			if(X[i].zhou - X[i - 1].zhou <= mid) {

				if(fin(X[i].ord) != fin(X[i - 1].ord)) {

					fa[fin(X[i].ord)] = fin(X[i - 1].ord);

					++cnt;

				}

			}

			if(Y[i].zhou - Y[i - 1].zhou <= mid) {

				if(fin(Y[i].ord) != fin(Y[i - 1].ord)) {

					fa[fin(Y[i].ord)] = fin(Y[i - 1].ord);

					++cnt;

				}

			}

		}

		if(cnt == N - 1)

            r = mid - 1;

		else l = mid + 1;

	}

	mid = r + 1;

	cnt = 0;

	for(int i = 2;i <= N;++i) {

		if(X[i].zhou - X[i - 1].zhou <= mid) {

			e[++cnt].u = X[i].ord;

			e[cnt].v = X[i - 1].ord;

			e[cnt].w = X[i].zhou - X[i - 1].zhou;

		}

		if(Y[i].zhou - Y[i - 1].zhou <= mid) {

			e[++cnt].u = Y[i].ord;

			e[cnt].v = Y[i - 1].ord;

			e[cnt].w = Y[i].zhou - Y[i - 1].zhou;

		}

	}

	int ans = 0;

	sort(e +1,e + 1 + cnt);

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

		fa[i] = i;

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

		if(fin(e[i].u) != fin(e[i].v)) {

			fa[fin(e[i].u)] = fin(e[i].v);

			ans += e[i].w;

		}

	}

	printf("%d\n",ans);



}