#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using dbl = double;
using pii = pair<int, int>;
using pl4 = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpl4 = vector<pl4>;
using vvpl4 = vector<vpl4>;
using vd = vector<dbl>;
using vvd = vector<vd>;
const ll MOD = 1e9 + 7;
const ll N = 1e5 + 20;
const ll INF = 1LL << 60;
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
class BIT {
 public:
  vll d;
  vll v;
  ll size;
  BIT(ll sizeget) {
    size = sizeget;
    d.assign(size + 1, 0);
    v.assign(size, 0);
  }
  BIT(vll cpyvec) { copy(cpyvec); }
  void add(ll index, ll num) {
    v[index] += num;
    index++;
    while (index <= size) {
      d[index] += num;
      index += ((index) & (-(index)));
    }
    return;
  }
  ll sum(ll index = INF) {
    index++;
    index = ((index < size) ? (index) : (size));
    ll ans = 0;
    while (index) {
      ans += d[index];
      index -= ((index) & (-(index)));
    }
    return ans;
  }
  void copy(vll cpyvec) {
    size = cpyvec.size();
    d.assign(size + 1, 0);
    v.assign(size, 0);
    for (ll(i) = (0); (i) <= ((size)-1); (i)++) add(i, cpyvec[i]);
  }
};
class UFT {
 public:
  ll size;
  ll mode;
  vll root;
  vll rank;
  UFT(ll sizeget, ll modeget = 0) {
    size = sizeget;
    mode = modeget;
    root.resize(size, 0);
    if (!mode) rank.resize(size, 0);
    iota((root).begin(), (root).end(), (0));
  }
  ll find(ll x) { return root[x] == x ? x : root[x] = find(root[x]); }
  bool same(ll x, ll y) { return find(x) == find(y); }
  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (mode)
      root[y] = x;
    else {
      if (rank[x] < rank[y]) {
        root[x] = y;
      } else {
        root[y] = x;
        if (rank[x] == rank[y]) rank[x]++;
      }
    }
  }
};
ll gcd(ll a, ll b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll inverse[N];
void init_inverse() {
  inverse[1] = 1;
  for (ll i = 2; i < N; i++)
    inverse[i] = (MOD - MOD / i) * inverse[MOD % i] % MOD;
}
pl4 Bezout(ll a, ll b) {
  if (b != 0) {
    pl4 xy;
    xy = Bezout(b, a % b);
    return make_pair(xy.second, xy.first - ((a / b) * xy.second));
  }
  if (b == 0) {
    return make_pair(1, 0);
  }
}
pl4 Bez(ll a, ll b, ll c) {
  pl4 xy;
  ll x, y, z, gc;
  xy = Bezout(a, b);
  gc = gcd(a, b);
  if (c % gc != 0) return make_pair(-1, -1);
  x = xy.first * (c / gc);
  y = xy.second * (c / gc);
  if (x < 0)
    z = ((((-x) % ((b / gc))) == 0) ? ((-x) / ((b / gc)))
                                    : (((-x) / ((b / gc))) + 1));
  if (x >= 0) z = -x / (b / gc);
  x += z * (b / gc);
  y -= z * (a / gc);
  return make_pair(x, y);
}
void salv(vll v) {
  cout << ("{");
  ;
  for (ll i = (0); i <= (v.size() - 1); i++) {
    cout << (v[i]);
    ;
    if (i != v.size() - 1) cout << (",");
    ;
  }
  cout << ("}") << endl;
}
ll DigS10(ll n) {
  ll m = 0;
  for (ll i = (0);
       i <= (((llabs(n) == 0) ? (1) : ((ll)(log10(double(llabs(n)))) + 1)) - 1);
       i++) {
    m += (ll)((llabs(n) % (ll)(pow(10.0, (double)(i + 1)))) /
              (ll)(pow(10.0, (double)i)));
  }
  return m;
}
ll isP(ll n) {
  if (n <= 1) return 0;
  for (ll i = (2); i <= ((ll)sqrt(n)); i++) {
    if (n % i == 0) return 0;
  }
  return 1;
}
vll FactM(1, 1);
vll FactMI(1, 1);
ll PowM(ll a, ll b) {
  ll ans = 1, x = (a % MOD);
  for (ll i = (0);
       i <= (((llabs(b) == 0) ? (1) : ((ll)(log2(double(llabs(b)))) + 1)) - 1);
       i++) {
    if ((((b) >> (i)) & 1) == 1) ans = (ans * x) % MOD;
    if (i != (((llabs(b) == 0) ? (1) : ((ll)(log2(double(llabs(b)))) + 1)) - 1))
      x = (x * x) % MOD;
  }
  return ans;
}
ll Mod(ll a) {
  a %= MOD;
  a = (a + MOD) % MOD;
  return a;
}
ll InvM(ll a) {
  a = Mod(a);
  a = PowM(a, MOD - 2);
  return a;
}
void CFactM(ll n) {
  if (FactM.size() <= n) {
    for (ll i = (FactM.size()); i <= (n); i++) {
      FactM.push_back((FactM[i - 1] * (i % MOD)) % MOD);
    }
  }
  return;
}
void CFactMI(ll n) {
  CFactM(n);
  if (FactMI.size() < (n + 1)) FactMI.resize(n + 1, -1);
  if (FactMI[n] == -1) FactMI[n] = PowM(FactM[n], MOD - 2);
  for (ll i = (n - 1); i >= (1); i--) {
    if (FactMI[i] != -1) break;
    FactMI[i] = ((FactMI[i + 1] * ((i + 1) % MOD)) % MOD);
  }
  return;
}
ll CombM(ll n, ll k) {
  if ((n < 0) || (k < 0)) return 0;
  if (n < k) return 0;
  if (n + 1 > FactMI.size()) CFactMI(n);
  return ((((FactMI[k] * FactMI[n - k]) % MOD) * FactM[n]) % MOD);
}
ll LIS(vll v, ll m = 0) {
  if (v.size() > 0) {
    ll ans = 0;
    vll dp(v.size(), INF);
    for (ll i = (0); i <= (v.size() - 1); i++) {
      dp[m ? (upper_bound((dp).begin(), (dp).end(), (v[i])) - (dp).begin())
           : (lower_bound((dp).begin(), (dp).end(), (v[i])) - (dp).begin())] =
          v[i];
    }
    for (ll i = (0); i <= (v.size() - 1); i++) {
      if (dp[i] == INF) break;
      ans++;
    }
    return ans;
  } else {
    return 0;
  }
}
void cmprs(vll& v) {
  if (v.size() == 0) return;
  vll vv(v);
  iota((vv).begin(), (vv).end(), (0));
  sort(vv.begin(), vv.end(), [&](ll v1, ll v2) { return v[v1] < v[v2]; });
  iota((v).begin(), (v).end(), (0));
  sort(v.begin(), v.end(), [&](ll v1, ll v2) { return vv[v1] < vv[v2]; });
  return;
}
ll BblCnt(vll v) {
  cmprs(v);
  BIT b(v.size());
  ll ans = 0;
  for (ll(i) = (0); (i) <= ((v.size()) - 1); (i)++) {
    ans += (i - b.sum(v[i]));
    b.add(v[i], 1);
  }
  return ans;
}
vll c(200000);
ll chk(vll c, ll a) {
  if (c[a] == a) {
    return a;
  } else {
    return chk(c, c[a]);
  }
}
signed main() {
  ll n, xx, yy, d, r = 0;
  cin >> n;
  vpl4 x(n), y(n), l(n);
  iota((c).begin(), (c).end(), (0));
  for (ll(i) = (0); (i) <= ((n)-1); (i)++) {
    cin >> xx >> yy;
    x[i] = make_pair(xx, i);
    y[i] = make_pair(yy, i);
    l[i] = make_pair(xx, yy);
  }
  sort((x).begin(), (x).end());
  sort((y).begin(), (y).end());
  vector<pair<ll, pair<ll, ll> > > s;
  for (ll(i) = (0); (i) <= ((n - 1) - 1); (i)++) {
    s.push_back(make_pair(x[i + 1].first - x[i].first,
                          make_pair(x[i].second, x[i + 1].second)));
  }
  for (ll(i) = (0); (i) <= ((n - 1) - 1); (i)++) {
    s.push_back(make_pair(y[i + 1].first - y[i].first,
                          make_pair(y[i].second, y[i + 1].second)));
  }
  sort((s).begin(), (s).end());
  for (ll(i) = (0); (i) <= ((2 * n - 2) - 1); (i)++) {
    if (chk(c, c[s[i].second.first]) != chk(c, c[s[i].second.second])) {
      r += s[i].first;
      c[s[i].second.first] = chk(c, c[s[i].second.second]);
    }
  }
  cout << r;
  return 0;
}
