#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long POW(long long x, long long n) {
  if (n == 0) return 1;
  if (n % 2 == 0) return POW(x * x, n / 2) % MOD;
  return x % MOD * POW(x, n - 1) % MOD;
}
long long POW2(long long x, long long n) {
  if (n == 0) return 1;
  if (n % 2 == 0) return POW2(x * x, n / 2);
  return x * POW2(x, n - 1);
}
long long POW3(long long x, long long n, long long m) {
  x %= m;
  if (n == 0) return 1;
  if (n % 2 == 0) return POW3(x * x, n / 2, m) % m;
  return x * POW3(x, n - 1, m) % m;
}
long long gcd(long long u, long long v) {
  long long r;
  while (0 != v) {
    r = u % v;
    u = v;
    v = r;
  }
  return u;
}
long long lcm(long long u, long long v) { return u * v / gcd(u, v); }
long long KAI(long long m) {
  if (m < 0) return 0;
  if (m == 0) return 1;
  return m * KAI(m - 1) % MOD;
}
long long KAI2(long long m) {
  if (m < 0) return 0;
  if (m == 0) return 1;
  return m * KAI2(m - 1);
}
long long extGCD(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long d = extGCD(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
inline long long mod(long long a, long long m) { return (a % m + m) % m; }
long long modinv(long long a) {
  long long x, y;
  extGCD(a, MOD, x, y);
  return mod(x, MOD);
}
long long COM(long long m, long long n) {
  if (m < n) return 0;
  if (n < 0) return 0;
  if (n == 0) return 1;
  if (m == n) return 1;
  return KAI(m) % MOD * modinv(KAI(n) % MOD * KAI(m - n) % MOD) % MOD;
}
long long COM2(long long m, long long n) {
  if (m < n) return 0;
  if (n < 0) return 0;
  if (n == 0) return 1;
  if (m == n) return 1;
  return KAI2(m) / KAI2(n) / KAI2(m - n);
}
long long DEC(long long x, long long m, long long n) {
  return x % POW(m, n + 1) / POW(m, n);
}
long long keta(long long x, long long n) {
  if (x == 0) return 0;
  return keta(x / n, n) + 1;
}
long long DIV(long long x, long long n) {
  if (x == 0) return 0;
  return x / n + DIV(x / n, n);
}
long long ORD(long long x, long long n) {
  if (x == 0) return (1e18);
  if (x % n != 0) return 0;
  return 1 + ORD(x / n, n);
}
long long SGS(long long x, long long y, long long m) {
  if (y == 0) return 0;
  if (y % 2 == 0) {
    return (1 + POW3(x, y / 2, m)) * SGS(x, y / 2, m) % m;
  }
  return (1 + x * SGS(x, y - 1, m)) % m;
}
long long SSGS(long long x, long long y, long long m) {
  if (y == 0) return 0;
  if (y == 1) return 1;
  if (y % 2 == 0) {
    return (SSGS(x, y / 2, m) * (POW3(x, y / 2, m) + 1) % m +
            SGS(x, y / 2, m) * y / 2 % m) %
           m;
  }
  return (SSGS(x, y - 1, m) * x % m + y) % m;
}
long long mincost[110000];
long long n, x[110000], y[110000];
bool used[110000];
int main() {
  cin >> n;
  vector<vector<long long>> q(n);
  for (long long i = 0; i < n; i++) cin >> x[i] >> y[i];
  vector<tuple<long long, long long, long long>> p(n);
  for (long long i = 0; i < n; i++) p[i] = make_tuple(x[i], y[i], i);
  sort((p).begin(), (p).end());
  for (long long i = 0; i < n - 1; i++) {
    q[get<2>(p[i])].push_back(get<2>(p[i + 1]));
    q[get<2>(p[i + 1])].push_back(get<2>(p[i]));
  }
  for (long long i = 0; i < n; i++) {
    swap(get<1>(p[i]), get<0>(p[i]));
  }
  sort((p).begin(), (p).end());
  for (long long i = 0; i < n - 1; i++) {
    q[get<2>(p[i])].push_back(get<2>(p[i + 1]));
    q[get<2>(p[i + 1])].push_back(get<2>(p[i]));
  }
  for (long long i = 0; i < n; i++) {
    swap(get<0>(p[i]), get<2>(p[i]));
  }
  sort((p).begin(), (p).end());
  for (long long i = 0; i < n; i++) {
    mincost[i] = (1e18);
    used[i] = false;
  }
  mincost[0] = 0;
  long long res = 0;
  while (1) {
    long long v = -1;
    for (long long i = 0; i < n; i++) {
      if (used[i] == false && (v == -1 || mincost[i] < mincost[v])) v = i;
    }
    if (v == -1) break;
    used[v] = true;
    res += mincost[v];
    for (long long i = 0; i < q[v].size(); i++) {
      mincost[q[v][i]] = min(
          {mincost[q[v][i]], abs(x[q[v][i]] - x[v]), abs(y[v] - y[q[v][i]])});
    }
  }
  printf("%lld", res);
}
