#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e12;
pair<long long, long long> x[100010];
pair<long long, long long> y[100010];
priority_queue<pair<long long, long long>> vt[100010];
bool used[100010];
long long mn[100010];
int main() {
  long long n;
  cin >> n;
  for (long long i = (0); i < (long long)n; i++) mn[i] = inf;
  for (long long i = (0); i < (long long)n; i++) {
    cin >> x[i].first >> y[i].first;
    x[i].second = i;
    y[i].second = i;
  }
  sort(x, x + n);
  sort(y, y + n);
  for (long long i = (0); i < (long long)n - 1; i++) {
    vt[x[i].second].push({x[i].first - x[i + 1].first, x[i + 1].second});
    vt[x[i + 1].second].push({x[i].first - x[i + 1].first, x[i].second});
    vt[y[i].second].push({y[i].first - y[i + 1].first, y[i + 1].second});
    vt[y[i + 1].second].push({y[i].first - y[i + 1].first, y[i].second});
  }
  long long ans = 0;
  vector<long long> ok;
  ok.push_back(0);
  ans += vt[0].top().first;
  used[0] = true;
  used[vt[0].top().second] = true;
  ok.push_back(vt[0].top().second);
  while ((long long)ok.size() != n) {
    long long node;
    long long mx = -inf;
    for (auto v : ok) {
      while (used[vt[v].top().second] && !vt[v].empty()) vt[v].pop();
      if (mx < vt[v].top().first) {
        mx = vt[v].top().first;
        node = vt[v].top().second;
      }
    }
    ans += mx;
    used[node] = true;
    ok.push_back(node);
  }
  cout << (-ans) << endl;
  return 0;
}
