#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, P> Pi;
int par[100002];
int rk[100002];
void init(int n){
    for(int i=0; i<n; i++){
        par[i]=i;
    }
}
int find(int x){
    if(par[x]==x){
        return x;
    }else{
        return par[x]=find(par[x]);
    }
}
void unite(int x, int y){
    x=find(x);
    y=find(y);
    if(x==y) return;
    if(rk[x]<rk[y]){
        par[x]=y;
    }else{
        par[y]=x;
        if(rk[x]==rk[y]) rk[x]++;
    }
}
bool same(int x, int y){
    return find(x)==find(y);
}
const int MAX=2e9+2;
struct node{
    int val, ind;
    int l, r;
    node *lch;
    node *rch;
    node():l(-MAX), r(MAX), val(MAX), ind(-1), lch(nullptr), rch(nullptr){}
    //node(int l, int r):l(l), r(r), val(0), lch(nullptr), rch(nullptr){}
};
node s[6200001];
int c;
void init(){
    for(int i=0; i<=c; i++){
        s[i].lch=nullptr;
        s[i].rch=nullptr;
        s[i].val=MAX, s[i].ind=-1;
    }
    c=0;
}
 
void update(int p, int a, int i, node* t){
    if(!(t->l<=p && p<t->r)) return;
    if(t->r-t->l==1){
        if(t->val>a){
            t->val=a, t->ind=i;
        }
    }else{
        if(!t->lch){
            c++;
            s[c].l=t->l, s[c].r=((ll)t->l+(ll)t->r)/2;
            t->lch=&s[c];
        }
        update(p, a, i, t->lch);
        if(!t->rch){
            c++;
            s[c].l=((ll)t->l+(ll)t->r)/2, s[c].r=t->r;
            t->rch=&s[c];
        }
        update(p, a, i, t->rch);
        if(t->lch->val>t->rch->val){
            t->ind=t->rch->ind, t->val=t->rch->val;
        }else{
            t->ind=t->lch->ind, t->val=t->lch->val;
        }
    }
}
 
P find(int a, int b, node* t){
    if(!t) return P(MAX, -1);
    if(b<=t->l || t->r<=a) return P(MAX, -1);
    if(a<=t->l && t->r<=b) return P(t->val, t->ind);
    P p1=find(a, b, t->lch), p2=find(a, b, t->rch);
    if(p1.first>p2.first) return p2;
    else return p1;
}
 
int main()
{
    int n; cin>>n;
    int ind[100001];
    int x[100001], y[100001];
    for(int i=0; i<n; i++){
        cin>>x[i]>>y[i];
        ind[i]=i;
    }
    vector<Pi> e;
 
	sort(ind, ind+n, [&](int i, int j){ return ((x[i]!=x[j])?(x[i]>x[j]):(y[i]>y[j]));});
    init();
    for(int i=0; i<n; i++){
        P p=find(y[ind[i]]-x[ind[i]], MAX, &s[0]);
        if(p.second>=0){
            e.push_back(Pi(max(abs(x[p.second]-x[ind[i]]), abs(y[p.second]-y[ind[i]])), P(p.second, ind[i])));
        }
        update(y[ind[i]]-x[ind[i]], y[ind[i]], ind[i], &s[0]);
    }
	init();
    for(int i=n-1; i>=0; i--){
        P p=find(-MAX, y[ind[i]]-x[ind[i]]+1, &s[0]);
        if(p.second>=0){
            e.push_back(Pi(max(abs(x[p.second]-x[ind[i]]), abs(y[p.second]-y[ind[i]])), P(p.second, ind[i])));
        }
        update(y[ind[i]]-x[ind[i]], -y[ind[i]], ind[i], &s[0]);
    }
	sort(ind, ind+n, [&](int i, int j){ return ((x[i]!=x[j])?(x[i]>x[j]):(y[i]<y[j]));});
    init();
    for(int i=0; i<n; i++){
        P p=find(-MAX, y[ind[i]]+x[ind[i]]+1, &s[0]);
        if(p.second>=0){
            e.push_back(Pi(max(abs(x[p.second]-x[ind[i]]), abs(y[p.second]-y[ind[i]])), P(p.second, ind[i])));
        }
        update(y[ind[i]]+x[ind[i]], -y[ind[i]], ind[i], &s[0]);
    }
    init();
    for(int i=n-1; i>=0; i--){
        P p=find(y[ind[i]]+x[ind[i]], MAX, &s[0]);
        if(p.second>=0){
            e.push_back(Pi(max(abs(x[p.second]-x[ind[i]]), abs(y[p.second]-y[ind[i]])), P(p.second, ind[i])));
        }
        update(y[ind[i]]+x[ind[i]], y[ind[i]], ind[i], &s[0]);
    }
 
    for(int i=0; i<n; i++) swap(x[i], y[i]);
    sort(ind, ind+n, [&](int i, int j){ return ((x[i]!=x[j])?(x[i]>x[j]):(y[i]>y[j]));});
    init();
    for(int i=0; i<n; i++){
        P p=find(y[ind[i]]-x[ind[i]], MAX, &s[0]);
        if(p.second>=0){
            e.push_back(Pi(max(abs(x[p.second]-x[ind[i]]), abs(y[p.second]-y[ind[i]])), P(p.second, ind[i])));
        }
        update(y[ind[i]]-x[ind[i]], y[ind[i]], ind[i], &s[0]);
    }
	init();
    for(int i=n-1; i>=0; i--){
        P p=find(-MAX, y[ind[i]]-x[ind[i]]+1, &s[0]);
        if(p.second>=0){
            e.push_back(Pi(max(abs(x[p.second]-x[ind[i]]), abs(y[p.second]-y[ind[i]])), P(p.second, ind[i])));
        }
        update(y[ind[i]]-x[ind[i]], -y[ind[i]], ind[i], &s[0]);
    }
	sort(ind, ind+n, [&](int i, int j){ return ((x[i]!=x[j])?(x[i]>x[j]):(y[i]<y[j]));});
    init();
    for(int i=0; i<n; i++){
        P p=find(-MAX, y[ind[i]]+x[ind[i]]+1, &s[0]);
        if(p.second>=0){
            e.push_back(Pi(max(abs(x[p.second]-x[ind[i]]), abs(y[p.second]-y[ind[i]])), P(p.second, ind[i])));
        }
        update(y[ind[i]]+x[ind[i]], -y[ind[i]], ind[i], &s[0]);
    }
    init();
    for(int i=n-1; i>=0; i--){
        P p=find(y[ind[i]]+x[ind[i]], MAX, &s[0]);
        if(p.second>=0){
            e.push_back(Pi(max(abs(x[p.second]-x[ind[i]]), abs(y[p.second]-y[ind[i]])), P(p.second, ind[i])));
        }
        update(y[ind[i]]+x[ind[i]], y[ind[i]], ind[i], &s[0]);
    }
 
    sort(e.begin(), e.end());
    init(n);
    ll ans=0;
    for(int i=0; i<e.size(); i++){
        int p=e[i].second.first, q=e[i].second.second;
        if(!same(p, q)){
            unite(p, q);
            ans+=e[i].first;
        }
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 214216