#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define dd double
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define fo(i , n) for(ll i = 0 ; i < n ; i++)
//#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<utility>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
double pi = acos(-1) ;
ll z =  998244353;
ll inf = 100000000000000000 ;
ll p1 = 37 ;
ll p2 = 53 ;
ll mod1 =  202976689 ;
ll mod2 =  203034253 ;
ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
ll power(ll a ,ll b , ll p){if(b == 0) return 1 ; ll c = power(a , b/2 , p) ; if(b%2 == 0) return ((c*c)%p) ; else return ((((c*c)%p)*a)%p) ;}
ll inverse(ll a ,ll n){return power(a , n-2 , n) ;}
ll max(ll a , ll b){if(a > b) return a ; return b ;}
ll min(ll a , ll b){if(a < b) return a ; return b ;}
ll left(ll i){return ((2*i)+1) ;}
ll right(ll i){return ((2*i) + 2) ;}
void swap(ll&a , ll&b){ll c = a ; a = b ; b = c ; return ;}
//ios_base::sync_with_stdio(0);
//cin.tie(0); cout.tie(0);
using namespace std ;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
//__builtin_popcount(n) -> returns number of set bits in n
int f[200005] ;
bool comp(pair<pair<ll ,ll> , ll> a , pair<pair<ll ,ll> , ll> b)
{
    if(a.first.second <= b.first.second)
        return 1 ;
    return 0 ;
}

ll find(ll u )
{
    if(f[u] == u)
        return u ;
    f[u] = find(f[u] ) ;
    return f[u] ;
}

void merge(ll u , ll v )
{
    ll a = find(u ) ;
    ll b = find(v ) ;
    f[a] = b ;
    return ;
}

void solve()
{
    ll n ;
    cin >> n ;
    vector<pair<ll , pair<ll ,ll> > > edges ;
    vector<pair<pair<ll , ll> , ll > >cord ;
    fo(i , n)
    {
        ll x , y ;
        cin >> x >> y ;
        pair<ll ,ll> g = mp(x,  y) ;
        cord.pub(mp(g , i)) ;
    }
    //exit(0) ;
    sort(cord.begin() , cord.end()) ;
    fo(i , n-1)
    {
        ll cost = abs(cord[i+1].first.first - cord[i].first.first) ;
        pair<ll ,ll> g = mp(cord[i].second , cord[i+1].second) ;
        edges.pub(mp(cost , g)) ;
    }
    sort(cord.begin() , cord.end() , comp) ;
    fo(i , n-1)
    {
        ll cost = abs(cord[i+1].first.second - cord[i].first.second );
        pair<ll ,ll> g = mp(cord[i].second , cord[i+1].second) ;
        edges.pub(mp(cost , g)) ;
    }
    sort(edges.begin() , edges.end()) ;
    /*fo(i , edges.size())
    {
        cout << edges[i].first << " " << edges[i].second.first << ' ' << edges[i].second.second << endl ;
    }
    */
    //exit(0) ; 
    
    fo(i , n)
    {
        f[i] = i ;
    }
    ll ans = 0 ;
    fo(i , edges.size())
    {
        ll c = edges[i].first ;
        ll u = edges[i].second.first ;
        ll v = edges[i].second.second ;
        if(find(u ) == find(v))
            continue ;
        else
        {
            merge(u , v) ;
            ans += c ;
        }
    }
    cout << ans << '\n' ;

    return ;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("inputf.txt" , "r" , stdin) ;
    freopen("outputf.txt" , "w" , stdout) ;
    freopen("error.txt" , "w" , stderr) ;
    #endif
    
    ll t ;
    //cin >> t ;
    t = 1 ;
    while(t--)
    {
        solve() ;
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
 
    return 0;
}