#include <bits/stdc++.h>
using namespace std;
long long longinf = 1ll << 60;
int inf = 1 << 29;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int dh[4] = {0, 1, 0, -1};
int dw[4] = {1, 0, -1, 0};
int main() {
  int N;
  cin >> N;
  pair<long long, long long> p[N];
  for (long long i = (long long)0ll; i < (long long)N; i++) {
    cin >> p[i].first >> p[i].second;
  }
  sort(p, p + N);
  map<pair<long long, long long>,
      vector<pair<long long, pair<long long, long long>>>>
      v;
  for (long long i = (long long)0ll; i < (long long)N; i++) {
    if (i != (N - 1)) {
      long long d = abs(p[i + 1].first - p[i].first);
      v[p[i]].push_back({d, p[i + 1]});
    }
    if (i != 0) {
      long long d = abs(p[i].first - p[i - 1].first);
      v[p[i]].push_back({d, p[i - 1]});
    }
  }
  for (long long i = (long long)0ll; i < (long long)N; i++) {
    swap(p[i].first, p[i].second);
  }
  sort(p, p + N);
  for (long long i = (long long)0ll; i < (long long)N; i++) {
    swap(p[i].first, p[i].second);
  }
  for (long long i = (long long)0ll; i < (long long)N; i++) {
    if (i != (N - 1)) {
      long long d = abs(p[i + 1].second - p[i].second);
      bool uu = 1;
      for (long long j = (long long)0ll; j < (long long)v[p[i]].size(); j++) {
        pair<long long, pair<long long, long long>> mochi = v[p[i]][j];
        long long dd = mochi.first;
        pair<long long, long long> point = mochi.second;
        if (p[i + 1] == point) {
          uu = 0;
          if (dd > d) v[p[i]][j].first = d;
        }
      }
      if (uu) v[p[i]].push_back({d, p[i + 1]});
    }
    if (i != 0) {
      long long d = abs(p[i].second - p[i - 1].second);
      bool uu = 1;
      for (long long j = (long long)0ll; j < (long long)v[p[i]].size(); j++) {
        pair<long long, pair<long long, long long>> mochi = v[p[i]][j];
        long long dd = mochi.first;
        pair<long long, long long> point = mochi.second;
        if (p[i - 1] == point) {
          uu = 0;
          if (dd > d) v[p[i]][j].first = d;
        }
      }
      if (uu) v[p[i]].push_back({d, p[i - 1]});
    }
  }
  map<pair<long long, long long>, long long> d;
  map<pair<long long, long long>, bool> merged;
  for (long long i = (long long)0ll; i < (long long)N; i++) {
    d[p[i]] = longinf;
    merged[p[i]] = false;
  }
  d[p[0]] = 0;
  priority_queue<pair<long long, pair<long long, long long>>,
                 vector<pair<long long, pair<long long, long long>>>,
                 greater<pair<long long, pair<long long, long long>>>>
      q;
  q.push({d[p[0]], p[0]});
  long long ans = 0;
  while (q.size()) {
    pair<long long, pair<long long, long long>> hoge = q.top();
    q.pop();
    long long di = hoge.first;
    pair<long long, long long> node = hoge.second;
    if (merged[node]) continue;
    ans += di;
    merged[node] = true;
    for (int i = 0; i < v[node].size(); i++) {
      long long nd = v[node][i].first;
      pair<long long, long long> np = v[node][i].second;
      d[np] = min(d[np], nd);
      q.push({d[np], np});
    }
  }
  cout << ans << endl;
  return 0;
}
