//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std;

#define FOR(i,f,t) for(int i=(int)f; i<(int)t; i++)
#define FORR(i,f,t) for(int i=(int)f; i>(int)t; i--)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define pb push_back
#define SYNC ios_base::sync_with_stdio(false)
#define inf 2248012
#define mp make_pair
#define F first
#define S second
#define ri(x) scanf("%d",&x)
#define rii(x,y) scanf("%d %d",&x,&y)
#define riii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define lri(x) scanf("%lld",&x)
#define lrii(x,y) scanf("%lld %lld",&x,&y)
#define lriii(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 5e5+3; //AREGLAAAAAAAAR!!!!
ll mod=1e9+7;
vector<pair<ll,int> > xs,ys;
map<pair<ll,pair<int,int> >,int> mapa;
int rep[MAXN],posx[MAXN],posy[MAXN];

int find(int x){
	if(x==rep[x]) return(x);
	else return rep[x]=find(rep[x]);
}


int main(){
	int n;
	ri(n);
	FOR(i,0,n){
		ll x,y;
		lrii(x,y);
		xs.pb({x,i});
		ys.pb({y,i});
	}
	sort(xs.begin(),xs.end());
	sort(ys.begin(),ys.end());
	
	FOR(i,0,n) posy[ys[i].S]=i;
	FOR(i,0,n) posx[xs[i].S]=i;
	
	priority_queue<pair<ll,pair<int,int> > > cola;
	
	cola.push({-(abs(xs[0].F-xs[1].F)),{max(xs[0].S,xs[1].S),min(xs[0].S,xs[1].S)}});
	cola.push({-(abs(ys[0].F-ys[1].F)),{max(ys[0].S,ys[1].S),min(ys[0].S,ys[1].S)}});
	
	FOR(i,0,n) rep[i]=i;
	ll ans=0; int N=n;
	while(!cola.empty()){
		int p=cola.top().S.F,p1=cola.top().S.S; 
		
		if(rep[find(p)]!=rep[find(p1)]){
			int dist=-cola.top().F; ans+=dist;
			cola.pop(); 
			int py=posy[p],py2=posy[p1],px=posx[p],px2=posx[p1];
			if(py+1<N){
				int ymax=ys[py+1].F,ymin=ys[py].F;
				int p2=ys[py+1].S,p3=ys[py].S;
				if(!mapa.count({-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}})){ 
					cola.push({-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}});
					mapa[{-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}}]=1;
				}
			}
			if(py-1>=0){
				int ymax=ys[py].F,ymin=ys[py-1].F;
				int p2=ys[py].S,p3=ys[py-1].S;
				if(!mapa.count({-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}})){ 
					cola.push({-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}});
					mapa[{-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}}]=1;
				}				
			}
			if(py2+1<N){
				int ymax=ys[py2+1].F,ymin=ys[py2].F;
				int p2=ys[py2+1].S,p3=ys[py2].S;
				if(!mapa.count({-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}})){ 
					cola.push({-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}});
					mapa[{-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}}]=1;
				}
			}
			if(py2-1>=0){
				int ymax=ys[py2].F,ymin=ys[py2-1].F;
				int p2=ys[py2].S,p3=ys[py2-1].S;
				if(!mapa.count({-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}})){ 
					cola.push({-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}});
					mapa[{-abs(ymax-ymin),{max(p2,p3),min(p2,p3)}}]=1;
				}				
			}
			
			if(px+1<N){
				int xmax=xs[px+1].F,xmin=xs[px].F;
				int p2=xs[px+1].S,p3=xs[px].S;
				if(!mapa.count({-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}})){
					cola.push({-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}});
					mapa[{-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}}]=1;
				}
			}
			if(px-1>=0){
				int xmax=xs[px].F,xmin=xs[px-1].F;
				int p2=xs[px].S,p3=xs[px-1].S;
				if(!mapa.count({-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}})){
					cola.push({-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}});
					mapa[{-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}}]=1;
				}
			}
			
			if(px2+1<N){
				int xmax=xs[px2+1].F,xmin=xs[px2].F;
				int p2=xs[px2+1].S,p3=xs[px2].S;
				if(!mapa.count({-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}})){
					cola.push({-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}});
					mapa[{-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}}]=1;
				}
			}
			if(px2-1>=0){
				int xmax=xs[px2].F,xmin=xs[px2-1].F;
				int p2=xs[px2].S,p3=xs[px2-1].S;
				if(!mapa.count({-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}})){
					cola.push({-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}});
					mapa[{-abs(xmax-xmin),{max(p2,p3),min(p2,p3)}}]=1;
				}
			}
			rep[find(p)]=find(p1);
		}else cola.pop();
	}
	printf("%lld\n",ans);

}


Measured memory_bytes: 20836