#include <bits/stdc++.h>
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
int qp(int a, ll b) {
  int ans = 1;
  do {
    if (b & 1) ans = 1ll * ans * a % 1000000007;
    a = 1ll * a * a % 1000000007;
  } while (b >>= 1);
  return ans;
}
int qp(int a, ll b, int mo) {
  int ans = 1;
  do {
    if (b & 1) ans = 1ll * ans * a % mo;
    a = 1ll * a * a % mo;
  } while (b >>= 1);
  return ans;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main(void) {
  ll n;
  cin >> n;
  vector<ll> x(n);
  vector<ll> y(n);
  vector<ll> mincost(n);
  vector<bool> used(n);
  for (int i = 0; i < (int)(n); i++) {
    cin >> x[i];
    cin >> y[i];
    mincost[i] = (1LL << (60));
    used[i] = false;
  }
  mincost[0] = 0;
  ll ans = 0;
  while (true) {
    ll v = -1;
    for (int u = 0; u < (int)(n); u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v])) {
        v = u;
      }
    }
    if (v == -1) {
      break;
    }
    used[v] = true;
    ans += mincost[v];
    for (int u = 0; u < (int)(n); u++) {
      if (u == v) continue;
      mincost[u] = min(mincost[u], min(abs(x[u] - x[v]), abs(y[u] - y[v])));
    }
  }
  cout << ans << endl;
  return 0;
}
