// Original C++ solution (slower):
#include <bits/stdc++.h>

#define be(v) (v).begin(),(v).end()

#define pb(q) push_back(q)

typedef long long ll;

using namespace std;

const ll mod=1000000007;

#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;



ll uni[100005];

ll root(ll u){

    if(uni[u]<0)return u;

    return uni[u]=root(uni[u]);

}



void connect(int a,int b){

    a=root(a);

    b=root(b);

    if(a==b)return;

    if(uni[a]<uni[b]){

        a^=b;

        b^=a;

        a^=b;

    }

    uni[a]+=uni[b];

    uni[b]=a;

    return;

}

ll size(ll u){

    return -uni[root(u)];

}

int main() {

    cin.tie(0);

    cout.tie(0);

    ios::sync_with_stdio(false);

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

    ll n,a,b;

    vector<pair<ll,ll> > v,s;

    cin>>n;

    map<pair<ll,ll>,ll> mp;

    for(int i=0;i<n;i++){

        cin>>a>>b;

        v.pb(make_pair(a,b));

        mp[make_pair(a,b)]=i;

        s.pb(make_pair(b,a));

    }

    sort(be(v));

    sort(be(s));

    multiset<pair<ll,pair<ll,ll> > > ms;

    ll d;

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

        d=min(abs(v[i+1].first-v[i].first),abs(v[i+1].second-v[i].second));

        ms.insert(make_pair(d,make_pair(mp[v[i]],mp[v[i+1]])));

    }

    for(int i=0;i<n;i++)swap(s[i].first,s[i].second);

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

        d=min(abs(s[i+1].first-s[i].first),abs(s[i+1].second-s[i].second));

        ms.insert(make_pair(d,make_pair(mp[s[i]],mp[s[i+1]])));

    }

    ll ans=0;

    multiset<pair<ll,pair<ll,ll> > > ::iterator itr;

    while(!ms.empty()){

        itr=ms.begin();

        d=(*itr).first;

        a=(*itr).second.first;

        b=(*itr).second.second;

        ms.erase(itr);

        if(root(a)!=root(b)){

            connect(a,b);

            ans+=d;

        }

    }

    cout << ans<<endl;



    return 0;

}


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

#define be(v) (v).begin(),(v).end()

#define pb(q) push_back(q)

typedef long long ll;

using namespace std;

const ll mod=1000000007, INF=(1LL<<60);

#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;



struct UnionFind {

    vector<int> par;

    UnionFind(int n) :par(n, -1) { }

    void init(int n) { par.assign(n, -1); }

    int root(int x) {

        if (par[x] < 0) return x;

        else return par[x] = root(par[x]);

    }

    bool connect(int x, int y) {

        x = root(x); y = root(y);

        if (x == y) return false;

        if (par[x] > par[y]) swap(x, y);

        par[x] += par[y];

        par[y] = x;

        return true;

    }

    int size(int x) {

        return -par[root(x)];

    }

};

int main() {

    cin.tie(0);

    cout.tie(0);

    ios::sync_with_stdio(false);

    ll ans = 0;

    ll n;

    cin >> n;

    UnionFind uf(n);

    ll a[n], b[n];

    vector<pair<pair<ll, ll> , ll > > x, y;

    for(int i=0;i<n;i++){

    	cin >> a[i] >> b[i];

    	x.push_back({{a[i], b[i]}, i});

    	y.push_back({{b[i], a[i]}, i});

    }

    sort(be(x));

    sort(be(y));

    multiset<pair<ll,pair<ll,ll> > > ms;

    for(int i=0;i<n;i++){

    	if(i==0){

    		ms.insert({min(abs(x[i].first.first - x[i+1].first.first), abs(x[i].first.second - x[i+1].first.second)), {x[i].second, x[i+1].second} });

    		ms.insert({min(abs(y[i].first.first - y[i+1].first.first), abs(y[i].first.second - y[i+1].first.second)), {y[i].second, y[i+1].second} });

    	}else if(i==n-1){

    		continue;

    	}else{

    		ms.insert({min(abs(x[i].first.first - x[i+1].first.first) , abs(x[i].first.second - x[i+1].first.second)), {x[i].second, x[i+1].second} });

    		ms.insert({min(abs(y[i].first.first - y[i+1].first.first) , abs(y[i].first.second - y[i+1].first.second)), {y[i].second, y[i+1].second} });

    	}

    }

    while(!ms.empty()){

    	pair<ll, pair<ll,ll> > niko = *(ms.begin());

    	ms.erase(ms.begin());

    	pair<ll,ll> maki = niko.second;

    	if(uf.root(maki.first) != uf.root(maki.second)){

    		uf.connect(maki.first, maki.second);

    		ans += niko.first;

    	}

    }

    cout << ans << endl;

    return 0;

}


// Fastest known solution for this problem: