#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    gcd(b, a % b);
}
unsigned long long pwr(unsigned long long base, unsigned long long p,
                       unsigned long long mod = (1000000007LL)) {
  unsigned long long ans = 1;
  while (p) {
    if (p & 1) ans = ((ans % mod) * (base % mod)) % mod;
    base = ((base % mod) * (base % mod)) % mod;
    p /= 2;
  }
  return ans;
}
const int N = (int)1e5 + 500;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<pair<long long, long long> > vx;
  vector<pair<long long, long long> > vy;
  for (long long i = 0; i < n; ++i) {
    long long x, y;
    cin >> x >> y;
    vx.push_back(make_pair(x, i));
    vy.push_back(make_pair(y, i));
  }
  long long cost;
  vector<tuple<long long, int, int> > edge;
  sort(vx.begin(), vx.end());
  sort(vy.begin(), vy.end());
  for (int i = 0; i < vx.size() - 1; ++i) {
    cost = abs(vx[i].first - vx[i + 1].first);
    edge.push_back(make_tuple(cost, vx[i].second, vx[i + 1].second));
  }
  for (int i = 0; i < vy.size() - 1; ++i) {
    cost = abs(vy[i].first - vy[i + 1].first);
    edge.push_back(make_tuple(cost, vy[i].second, vy[i + 1].second));
  }
  sort(edge.begin(), edge.end());
  int nodes[n];
  for (int i = 0; i < n; ++i) {
    nodes[i] = 0;
  }
  long long res = 0, tot = 0;
  for (int i = 0; i < edge.size(); ++i) {
    long long d;
    int a, b;
    tie(d, a, b) = edge[i];
    if (nodes[a] == 0 or nodes[a] == 1 && nodes[b] == 0 or nodes[b] == 1) {
      res += d;
      tot++;
      nodes[a]++;
      nodes[b]++;
    }
    if (tot == (n - 1)) break;
  }
  cout << res << endl;
  return 0;
}
