#include<bits/stdc++.h>
using namespace std;
//type
typedef long long ll;
#define pii pair<int, int>
#define vi vector<int>
//x * y * 1.0 can cause overflow
//constant
#define inf (int)(1e9+7)
#define mod (ll)(1e9+7)
#define eps 1e-10
//omission
#define eb emplace_back
#define F first
#define S second
#define SZ(x) ((int)(x).size())
#define all(v) v.begin(), v.end()
#define ios cin.tie(0); ios::sync_with_stdio(false)
#define FIX(a) cout << fixed << setprecision(a)
//manip
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end(), v.end());
#define fill(x, y) memset(x, y, sizeof(x))
#define ceil(a, b) a / b + !!(a % b)
template<class T> T power(T a, T b)
{return b ? power(a * a % inf, b / 2) * (b % 2 ? a : 1) % inf : 1;}
#define LB(v, x) (int)(lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (int)(upper_bound(v.begin(), v.end(), x) - v.begin())
template<typename T> T gcd(T a, T b) {if (b == 0) return a; return gcd(b, a % b);}
template<typename T> T lcm(T a, T b) {return a / gcd(max(a, b), min(a, b)) * b;} //先に割る!!
#define outa(a, n) rep(i, n) cout << a[i] << " "; cout << endl
#define out(a) cout << a << endl
#define outv(v) rep(i, SZ(v)) cout << v[i] << " "; cout << endl
//loop
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep3(i, st, n) for (int i = st; i < n; ++i)
//algorithm
//double pointer, l start, how many adds, can be 0 -> init r = l, sum = 0
//bfs, not x, y, i, j
//not !(i % 2), i % 2 == 0
//minimal required terms
//cast caution
//look constraints always


/*
 最小全域木
 
 どうやってcostでsortするかがpointかな
 
 組み合わせ考えたらN^2
 
 解説見た
 ある点と、その点から2本以上の辺を通って着く点との距離は、すべての点で隣だけ見たら求まる
 
 cost = min(|a - c|, |b - d|)より
 
 
 
 */



class UnionFind {
public:
    vector<int> Parent;
    UnionFind(int N) {
        Parent = vector<int>(N, -1);
    }
    int root(int A) {
        if (Parent[A] < 0) return A;
        return Parent[A] = root(Parent[A]);
    }
    int size(int A) {
        return -Parent[root(A)];
    }
    bool connect(int A, int B) {
        A = root(A);
        B = root(B);
        if (A == B) {
            return true;
        }
        if (size(A) < size(B)) swap(A, B);
        Parent[A] += Parent[B];
        Parent[B] = A;
        return false;
    }
};


bool comp(pair<int, pii> a, pair<int, pii> b) {
    return a.S.F < b.S.F;
}
bool comp2(pair<int, pii> a, pair<int, pii> b) {
    return a.S.S < b.S.S;
}
bool compv(pair<pii, int> a, pair<pii, int> b) {
    return a.S < b.S;
}

int main() {
    
    
    
    
    int n; cin >> n;
    pair<int, pii> p[n];
    rep(i, n) {cin >> p[i].S.F >> p[i].S.S; p[i].F = i;}
    
    sort(p, p + n, comp);
    vector<pair<pii, int> > v; //id1, id2, cost
    rep3(i, 1, n) {
        v.eb(pii(p[i].F, p[i - 1].F), p[i].S.F - p[i - 1].S.F);
    }
    sort(p, p + n, comp2);
    //cout << endl; rep(i, n) cout << p[i].F << " " << p[i].S.F << " " << p[i].S.S << endl;
    rep3(i, 1, n) {
        v.eb(pii(p[i].F, p[i - 1].F), p[i].S.S - p[i - 1].S.S);
    }
    sort(all(v), compv);
    
    //cout << endl; rep(i, SZ(v)) cout << v[i].F.F << " " << v[i].F.S << " " << v[i].S << endl;
    
    
    
    UnionFind uni(n);
    ll ans = 0;
    rep(i, SZ(v)) {
        if (uni.connect(v[i].F.F, v[i].F.S) == 1) continue;
        //cout << v[i].F.F << " " << v[i].F.S << " " << v[i].S << endl;
        ans += v[i].S;
    }
    out(ans);
    
    
}

#include <bits/stdc++.h>

using namespace std;

const int N = 200010;

int n;
int p[N];
pair<pair<int, int>, int> c[N];
pair< int, pair<int, int> > e[N];

int group(int a) {
  if (p[a] == -1) {
    return a;
  }
  return p[a] = group(p[a]);
}

int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    c[i] = make_pair(make_pair(x, y), i);
  }
  for (int it = 0; it < 2; it++) {
    sort(c, c + n);
    for (int i = 0; i < n - 1; i++) {
      int x = c[i].second;
      int y = c[i + 1].second;
      int d = abs(c[i + 1].first.first - c[i].first.first);
      e[it * (n - 1) + i] = make_pair(d, make_pair(x, y));
    }
    for (int i = 0; i < n; i++) {
      swap(c[i].first.first, c[i].first.second);
    }
  }
  sort(e, e + 2 * n - 2);
  fill_n(p, n, -1);
  long long ans = 0;
  for (int i = 0; i < 2 * n - 2; i++) {
    int x = group(e[i].second.first);
    int y = group(e[i].second.second);
    int d = e[i].first;
    if (x == y) {
      continue;
    }
    p[x] = y;
    ans += d;
  }
  printf("%lld\n", ans);
  return 0;
}