#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long to, cost;
};
using P = pair<long long, long long>;
using PP = pair<P, long long>;
using D = pair<long double, long double>;
const long long mod2 = 998244353;
const long long mod = (long long)1e9 + 7, inf = (long long)1e16;
const string sp = " ";
long long modpow(long long x, long long n) {
  long long ans = 1;
  while (n > 0) {
    if (n & 1) {
      ans *= x;
      if (mod <= ans) ans %= mod;
    }
    x *= x;
    if (mod <= x) x %= mod;
    n >>= 1;
  }
  return ans;
}
long long fact[1000000];
void f_init(long long n) {
  if (1000000 <= n) return;
  fact[0] = fact[1] = 1;
  for (long long i = 2; i <= n; ++i) {
    fact[i] *= fact[i - 1];
    if (mod <= fact[i]) fact[i] %= mod;
  }
  return;
}
long long comb(long long n, long long r) {
  if (n < r) return 0;
  long long ans =
      fact[n] * modpow(fact[n - r], mod - 2) % mod * modpow(fact[r], mod - 2);
  if (ans < 0) return ans + mod;
  return ans;
}
long long perm(long long n, long long r) {
  if (n < r) return 0;
  if (n - r < r) r = n - r;
  long long ans = fact[n] * modpow(fact[n - r], mod - 2) % mod;
  if (ans < 0) return ans + mod;
  return ans;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
bool pri(long long p) {
  for (long long i = 2; i * i <= p; ++i)
    if (p % i == 0) return false;
  return p > 1;
}
map<long long, long long> factring(long long n) {
  map<long long, long long> ans;
  for (long long i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      n /= i;
      ++ans[i];
    }
  }
  if (n != 1) ++ans[n];
  return ans;
}
P operator+(const P& a, const P& b) {
  return P(a.first + b.first, a.second + b.second);
}
P operator-(const P& a, const P& b) {
  return P(a.first - b.first, a.second - b.second);
}
class unionfind {
 private:
  long long par[200010], rankth[200010], sizeth[200020];

 public:
  void init(long long n) {
    for (long long i = 0; i < (long long)(n); ++i) {
      par[i] = i;
      rankth[i] = 0;
      sizeth[i] = 1;
    }
  }
  long long root(long long x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool same(long long x, long long y) { return root(x) == root(y); }
  void unite(long long x, long long y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (rankth[x] < rankth[y]) {
      par[x] = y;
      sizeth[y] += sizeth[x];
    } else {
      par[y] = x;
      if (rankth[x] == rankth[y]) rankth[x]++;
      sizeth[x] += sizeth[y];
    }
  }
};
long long n;
P x[100000], y[100000];
priority_queue<PP, vector<PP>, greater<PP>> que;
long long ans;
signed main() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> x[i].first >> y[i].first;
    x[i].second = i + 1;
    y[i].second = i + 1;
  }
  sort(x, x + n);
  sort(y, y + n);
  for (long long i = 1; i < n; i++) {
    que.push({{x[i].first - x[i - 1].first, x[i - 1].second}, x[i].second});
    que.push({{y[i].first - y[i - 1].first, y[i - 1].second}, y[i].second});
  }
  unionfind UF;
  UF.init(n);
  while ((long long)que.size()) {
    PP p = que.top();
    que.pop();
    if (!UF.same(p.first.second, p.second)) {
      ans += p.first.first;
      UF.unite(p.first.second, p.second);
    }
  }
  cout << ans << endl;
}
