#include <bits/stdc++.h>
template <typename T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long int;
const int MOD = 1000000007;
const int INTMAX = 2147483647;
const ll LLMAX = 9223372036854775807;
bool comp(vector<ll> a, vector<ll> b) { return a[0] < b[0]; }
ll prim(vector<vector<ll> > cost, ll N) {
  vector<ll> mincost(N);
  vector<bool> done(N);
  for (ll i = 0; i < N; ++i) {
    mincost[i] = LLMAX;
    done[i] = false;
  }
  mincost[0] = 0;
  ll res = 0;
  while (true) {
    ll v = -1;
    for (ll i = 0; i < N; i++) {
      if (!done[i] && (v == -1 || mincost[i] < mincost[v])) v = i;
    }
    if (v == -1) break;
    done[v] = true;
    res += mincost[v];
    for (ll i = 0; i < N; i++) {
      mincost[i] = min(mincost[i], cost[v][i]);
    }
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N;
  cin >> N;
  vector<vector<ll> > x(N, vector<ll>(2)), y(N, vector<ll>(2));
  for (ll i = 0; i < N; i++) {
    cin >> x[i][0] >> y[i][0];
    x[i][1] = i;
    y[i][1] = i;
  }
  sort(x.begin(), x.end(), comp);
  sort(y.begin(), y.end(), comp);
  vector<vector<ll> > cost(N, vector<ll>(N, LLMAX));
  for (ll i = 0; i < N - 1; i++) {
    cost[x[i][1]][x[i + 1][1]] = x[i + 1][0] - x[i][0];
    cost[x[i + 1][1]][x[i][1]] = x[i + 1][0] - x[i][0];
  }
  for (ll i = 0; i < N - 1; i++) {
    cost[y[i][1]][y[i + 1][1]] = y[i + 1][0] - y[i][0];
    cost[y[i + 1][1]][y[i][1]] = y[i + 1][0] - y[i][0];
  }
  cout << prim(cost, N) << endl;
  return 0;
}
