#include <bits/stdc++.h>
using namespace std;
int N;
long long x[2][100010];
int main() {
  cin >> N;
  for (auto i = 0; i < N; ++i) {
    cin >> x[0][i] >> x[1][i];
  }
  vector<tuple<long long, int, int> > V;
  for (auto k = 0; k < 2; ++k) {
    vector<tuple<int, int> > W;
    for (auto i = 0; i < N; ++i) {
      W.push_back(tuple<int, int>(x[k][i], i));
    }
    sort(W.begin(), W.end());
    for (auto i = 0; i < N - 1; ++i) {
      int d0 = get<0>(W[i]);
      int p0 = get<1>(W[i]);
      int d1 = get<0>(W[i + 1]);
      int p1 = get<1>(W[i + 1]);
      V.push_back(tuple<long long, int, int>(abs(d0 - d1), p0, p1));
    }
  }
  sort(V.begin(), V.end());
  int used = 0;
  long long ans = 0;
  bool visited[100010];
  fill(visited, visited + 100010, false);
  auto it = V.begin();
  while (used < N) {
    long long cost = get<0>(*it);
    int p0 = get<1>(*it);
    int p1 = get<2>(*it);
    ++it;
    if (visited[p0] && visited[p1]) continue;
    if (!visited[p0]) {
      visited[p0] = true;
      ++used;
    }
    if (!visited[p1]) {
      visited[p1] = true;
      ++used;
    }
    ans += cost;
  }
  cout << ans << endl;
}
