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

#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;

const int maxN=1e5+5;



int fa[maxN];



struct node

{

    int x,y,id;

}a[maxN],b[maxN];



struct edge

{

    int f,t,w;

}e[maxN*2];



bool cmp1(node x1,node x2)

{

    return x1.x<x2.x;

}



bool cmp2(node x1,node x2)

{

    return x1.y<x2.y;

}



bool cmp3(edge x1,edge x2)

{

    return x1.w<x2.w;

}



int find(int x)

{

    if(fa[x]==-1)return x;

    return fa[x]=find(fa[x]);

}



int uni(int f,int t)

{

    f=find(f);t=find(t);

    if(f==t)return 0;

    fa[f]=t;return 1;

}



int main()

{

    int k,n,cnt=0,now=0;

    long long ans=0;

    memset(fa,-1,sizeof(fa));

    scanf("%d",&n);

    for(k=0;k<n;k++)

    {

        scanf("%d%d",&a[k].x,&a[k].y);

        a[k].id=k;b[k].id=k;

        b[k].x=a[k].x;

        b[k].y=a[k].y;

    }

    sort(a,a+n,cmp1);

    sort(b,b+n,cmp2);

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

    {

        e[cnt].f=a[k-1].id;

        e[cnt].t=a[k].id;

        e[cnt].w=a[k].x-a[k-1].x;

        cnt++;

    }

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

    {

        e[cnt].f=b[k-1].id;

        e[cnt].t=b[k].id;

        e[cnt].w=b[k].y-b[k-1].y;

        cnt++;

    }

    sort(e,e+cnt,cmp3);

    for(k=0;k<cnt;k++)

    {

        if(uni(e[k].f,e[k].t))

        {

            ans+=e[k].w;

            now++;

        }

        if(now==n-1)break;

    }

    cout<<ans;

    return 0;

}


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

  

using namespace std;

  

template<class T> void ckmin(T &a, T b) { a = min(a, b); }

template<class T> void ckmax(T &a, T b) { a = max(a, b); }

  

#define pb push_back

#define mp make_pair

#define cotu cout

#define itn int

#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

#define F first

#define S second

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

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

#define rep(i, n) for(int i = 0; i < n; ++i)

#define repr(i,n) for(int i = n - 1; i >= 0; --i)

#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)

#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)

#define Repr(i, a, n) for(int i = (n); i >= (a); --i)

#define setp(x) fixed << setprecision(x)

#define ordered_set tree<int , null_type,less<int >, rb_tree_tag,tree_order_statistics_node_update> 

  

#include <ext/pb_ds/assoc_container.hpp> 

#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds; 

  

typedef long long ll;

typedef pair<ll, ll> pll;

typedef pair<int, int> pii;

  

const int inf = int(1e9);

const int mod = inf + 7;

const int N = 1e6 + 555; // TL MB

const double PI = acos(-1.0);

void mul(ll &x, ll y){

	x *= y;

	if(x >= mod) x %= mod;

}

 

void add(ll &x, ll y){

	x += y;

	if(x >= mod) x -= mod;

	if(x < 0) x += mod;

}

ll bp(ll a, ll n){

	ll r = 1;

	while(n){

		if(n & 1) mul(r, a);

		mul(a, a);

		n >>= 1;

	}

 

}



int p[N];

int get(int v){

	return v == p[v] ? v : p[v] = get(p[v]);

}

void solve()

{

	int n;

	cin >> n;

	vector<pii> x(n), y(n);

	set<pair<int, pii> > px, py;

	rep(i, n) p[i] = i;

	rep(i, n){

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

		x[i].S = y[i].S = i;

	}

	sort(all(x));

	sort(all(y));

	rep(i, n){

		if(i){

			px.insert({x[i].F - x[i - 1].F, {x[i].S, x[i - 1].S}});

			py.insert({y[i].F - y[i - 1].F, {y[i].S, y[i - 1].S}});

		}

	}

	ll res = 0;

	while(!px.empty() && !py.empty()){

		pair<int, pii> u = *px.begin();

		pair<int, pii> v = *py.begin();

		if(u.F <= v.F){

			res += 1ll * u.F;

			int x = get(u.S.F), y = get(u.S.S);

			p[y] = x;

			px.erase(px.begin());

		}

		else{

			res += 1ll * v.F;

			int x = get(v.S.F), y = get(v.S.S);

			p[y] = x;

			py.erase(py.begin());	

		}

		while(!px.empty()){

			pair<int, pii> x = *px.begin();

			if(get(x.S.F) == get(x.S.S)) px.erase(px.begin());

			else break;

		}

		while(!py.empty()){

			pair<int, pii> x = *py.begin();

			if(get(x.S.F) == get(x.S.S)) py.erase(py.begin());

			else break;

		}

	}

	cout << res;





}	 



 

int main()

{

	// freopen("input.txt", "r", stdin);

	// freopen("output.txt", "w", stdout);

    Red;

  	int T = 1;

  	// cin >> T;

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

  		solve();

  	}

  	return 0;



}