#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
class Solution {
 public:
  LL solve(int N, vector<int> &x, vector<int> &y) {
    set<tuple<int, int, int>> ox, oy;
    for (int i = 0; i < N; ++i) {
      ox.emplace(x[i], y[i], i);
      oy.emplace(y[i], x[i], i);
    }
    LL L = 0;
    vector<int> visited{0};
    while (ox.size() > 1) {
      ox.erase(
          make_tuple(x[visited.back()], y[visited.back()], visited.back()));
      oy.erase(
          make_tuple(y[visited.back()], x[visited.back()], visited.back()));
      int M = numeric_limits<int>::max(), m;
      int index = 0;
      for (auto i : visited) {
        auto p1 = make_tuple(x[i], y[i], i);
        auto p2 = make_tuple(y[i], x[i], i);
        auto nit1 = ox.upper_bound(p1);
        if (nit1 != ox.begin()) {
          auto pit1 = prev(nit1);
          int m = x[i] - get<0>(*pit1);
          if (m < M) {
            M = m;
            index = get<2>(*pit1);
          }
        }
        if (nit1 != ox.end()) {
          int m = get<0>(*nit1) - x[i];
          if (m < M) {
            M = m;
            index = get<2>(*nit1);
          }
        }
        auto nit2 = oy.upper_bound(p2);
        if (nit2 != oy.begin()) {
          auto pit2 = prev(nit2);
          int m = y[i] - get<0>(*pit2);
          if (m < M) {
            M = m;
            index = get<2>(*pit2);
          }
        }
        if (nit2 != oy.end()) {
          int m = get<0>(*nit2) - y[i];
          if (m < M) {
            M = m;
            index = get<2>(*nit2);
          }
        }
      }
      L += M;
      visited.push_back(index);
    }
    return L;
  }
};
int main() {
  Solution s;
  int N;
  while (cin >> N) {
    vector<int> x(N), y(N);
    for (int i = 0; i < N; ++i) {
      cin >> x[i] >> y[i];
    }
    cout << s.solve(N, x, y) << endl;
  }
  return 0;
}
