#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX / 4;
const int max_n = 1e3;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  priority_queue<pair<int, int>> X, Y;
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      q;
  int x, y;
  for (int i = 0; i < N; i++) {
    cin >> x >> y;
    X.push(make_pair(x, i));
    Y.push(make_pair(y, i));
  }
  pair<int, int> x1, x2;
  x1 = X.top();
  X.pop();
  while (!X.empty()) {
    x2 = X.top();
    X.pop();
    q.push(make_tuple(abs(x1.first - x2.first), x1.second, x2.second));
    x1 = x2;
  }
  pair<int, int> y1, y2;
  y1 = Y.top();
  Y.pop();
  while (!Y.empty()) {
    y2 = Y.top();
    Y.pop();
    q.push(make_tuple(abs(y1.first - y2.first), y1.second, y2.second));
    y1 = y2;
  }
  long long cost = 0;
  bool connected[N];
  for (int i = 0; i < N; i++) connected[i] = false;
  tuple<int, int, int> t;
  int c, t1, t2;
  while (!q.empty()) {
    t = q.top();
    q.pop();
    c = get<0>(t);
    t1 = get<1>(t);
    t2 = get<2>(t);
    cerr << c << " " << t1 << " " << t2 << "\n";
    if ((!connected[t1]) || !(connected[t2])) {
      connected[t1] = true;
      connected[t2] = true;
      cost += c;
    }
  }
  cout << cost << "\n";
  return 0;
}
