#include <bits/stdc++.h>
using namespace std;
const long long INF =
    sizeof(long long) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const long long MOD = (long long)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long W(1), B(0);
vector<vector<pair<long long, long long>>> M;
long long n;
long long prim() {
  vector<long long> d(n, INF), color(n, W);
  d[0] = 0;
  priority_queue<pair<long long, long long>> pq;
  pq.push(make_pair(0, 0));
  while (!pq.empty()) {
    pair<long long, long long> f(pq.top());
    pq.pop();
    color[f.second] = B;
    if (d[f.second] < f.first * (-1)) continue;
    for (long long i = (0); i < (M[f.second].size()); i++) {
      pair<long long, long long> u(M[f.second][i]);
      if (color[u.first] != B && u.second < d[u.first]) {
        d[u.first] = u.second;
        pq.push(make_pair(d[u.first] * (-1), u.first));
      }
    }
  }
  return accumulate(begin(d), end(d), 0);
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N;
  cin >> N;
  M = vector<vector<pair<long long, long long>>>(
      N, vector<pair<long long, long long>>());
  vector<long long> x(N), y(N);
  for (long long i = (0); i < (N); i++) {
    cin >> x[i] >> y[i];
  }
  vector<pair<long long, long long>> v, h;
  map<pair<long long, long long>, long long> mp;
  for (long long i = (0); i < (N); i++) {
    v.emplace_back(make_pair(x[i], y[i]));
    h.emplace_back(make_pair(x[i], y[i]));
    mp[v[i]] = i;
  }
  vector<pair<long long, long long>> org(v);
  sort(begin(v), end(v),
       [](pair<long long, long long> a, pair<long long, long long> b) {
         return a.second < b.second;
       });
  sort(begin(h), end(h),
       [](pair<long long, long long> a, pair<long long, long long> b) {
         return a.first < b.first;
       });
  for (long long i = (0); i < (N); i++) {
    long long x = mp[v[i]];
    if (i - 1 >= 0) {
      long long c = abs(v[i].second - v[i - 1].second);
      long long y = mp[v[i - 1]];
      M[x].emplace_back(make_pair(y, c));
    }
    if (i + 1 < N) {
      long long c = abs(v[i + 1].second - v[i].second);
      long long y = mp[v[i + 1]];
      M[x].emplace_back(make_pair(y, c));
    }
  }
  for (long long i = (0); i < (N); i++) {
    long long x = mp[h[i]];
    if (i - 1 >= 0) {
      long long c = abs(h[i].first - h[i - 1].first);
      long long y = mp[h[i - 1]];
      M[x].emplace_back(make_pair(y, c));
    }
    if (i + 1 < N) {
      long long c = abs(h[i + 1].first - h[i].first);
      long long y = mp[h[i + 1]];
      M[x].emplace_back(make_pair(y, c));
    }
  }
  cout << prim() << endl;
  return 0;
}
