//code by lynmisakura.wish to be accepted!
/****************************/
#include<iostream>
#include<iomanip>
#include<math.h>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<bitset>
#include <climits>
#include<set>
#include<bitset>
using namespace std;
/***************************/
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<pair<int,int>> vpi;

const long long INF = 1LL << 55;

#define itn int
#define endl '\n'
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define mins(x,y) x = min(x,y)
#define maxs(x,y) x = max(x,y)
#define all(x) (x).begin(),(x).end()
#define Rep(n) for(int i = 0;i < n;i++)
#define rep(i,n) for(int i = 0;i < n;i++)
#define rrep(i,n) for(int i = n - 1;i >= 0;i--)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() )
 
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll qp(ll a, ll b) { ll ans = 1; do { if (b & 1)ans = 1ll * ans*a; a = 1ll * a*a; } while (b >>= 1); return ans; }
ll qp(ll a, ll b, int mo) { ll ans = 1; do { if (b & 1)ans = 1ll * ans*a%mo; a = 1ll * a*a%mo; } while (b >>= 1); return ans; }
 
#define _GLIBCXX_DEBUG
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

//mint
const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x(x%mod){}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

#define MAX_N 100010
#define MAX_E 400040
//クラスカル法による最小全域木
int par[MAX_N];
int ufrank[MAX_N];
int ufsize[MAX_N];
void init(int ufsize_of_tree){
    for (int i = 0; i < ufsize_of_tree; i++)
    {
        par[i] = i;
        ufrank[i] = 0;
        ufsize[i] = 1;
    } 
}
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(ufrank[x] < ufrank[y]){
        par[x] = y;
    int sx = ufsize[x], sy = ufsize[y];
    ufsize[x] = ufsize[y] = sx + sy;
    }else{
        par[y] = x;
        if(ufrank[x] == ufrank[y]) ufrank[x]++;
        int sx = ufsize[x], sy = ufsize[y];
        ufsize[x] = ufsize[y] = sx + sy;
    }
}
bool same(int x,int y){
    return find(x) == find(y);
}

struct edge{int u,v,cost;};
bool comp(const edge& e1,const edge& e2){
    return e1.cost < e2.cost;
}
edge es[MAX_E];
int V,E; //頂点数と辺数

ll kruskal(){
    sort(es,es + E,comp); //edge.costが小さい順にソート
    init(V);
    ll res = 0;
    
    for(int i = 0;i < E;i++){
        edge e = es[i];
        if(!same(e.u,e.v)){
            unite(e.u,e.v);
            res += (ll)e.cost;
        }
    }
    return res;
}

int main(void){
   int n;cin >> n;
   vector<pair<int,int>> a(n),b(n);
   rep(i,n) cin >> a[i].ff >> a[i].ss;
   rep(i,n){
      b[i].ff = a[i].ss,b[i].ss = a[i].ff;
   }
   sort(all(a));
   sort(all(b));
   
   int num = 0;
   rep(i,n-1){
      edge e1 = {i,i+1,min(abs(a[i].ff - a[i+1].ff),abs(a[i].ss - a[i+1].ss))};
      edge e2 = {i+1,i,min(abs(a[i].ff - a[i+1].ff),abs(a[i].ss - a[i+1].ss))};
      
      pair<int,int> key;
      key.ff = b[i].ss;
      key.ss = b[i].ff;
      pair<int,int> next;
      next.ff = b[i+1].ss;
      next.ss = b[i+1].ff;
      
      int x = lower_bound(a.begin(),a.end(),key) - a.begin();
      int y = lower_bound(a.begin(),a.end(),next) - a.begin();
      
      edge e3 = {x,y,min(abs(a[x].ff - a[y].ff),abs(a[x].ss - a[y].ss))};
      edge e4 = {y,x,min(abs(a[x].ff - a[y].ff),abs(a[x].ss - a[y].ss))};
      
      es[num++] = e1;
      es[num++] = e2;
      es[num++] = e3;
      es[num++] = e4;
   }
   V = n,E = 4*(n-1);
   cout << kruskal() << endl;
   return 0;
}

Measured memory_bytes: 7680