#include <bits/stdc++.h>
using namespace std;
typedef priority_queue<long long, vector<long long>> decendingQueue;
typedef priority_queue<long long, vector<long long>, greater<long long>>
    ascendingQueue;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const long long INF = 9 * 1e18;
const long long MOD = 1e9 + 7;
class T {
 public:
  long long from;
  long long to;
  long long cost;
  T(long long from, long long to, long long cost) {
    this->from = from;
    this->to = to;
    this->cost = cost;
  }
};
bool cmp(const T &a, const T &b) { return a.cost < b.cost; }
void solve(long long N, std::vector<long long> x, std::vector<long long> y) {
  vector<pair<long long, long long>> xi;
  vector<pair<long long, long long>> yi;
  for (int(i) = (0); (i) < (N); ++(i)) {
    xi.push_back(make_pair(x[i], i));
    yi.push_back(make_pair(y[i], i));
  }
  sort((xi).begin(), (xi).end());
  sort((yi).begin(), (yi).end());
  vector<T> edges;
  for (int(i) = (0); (i) < (N - 1); ++(i)) {
    long long xi1 = xi[i].second;
    long long xi2 = xi[i + 1].second;
    long long cost = min(abs(x[xi1] - x[xi2]), abs(y[xi1] - y[xi2]));
    edges.push_back(T(xi1, xi2, cost));
    long long yi1 = yi[i].second;
    long long yi2 = yi[i + 1].second;
    cost = min(abs(x[yi1] - x[yi2]), abs(y[yi1] - y[yi2]));
    edges.push_back(T(yi1, yi2, cost));
  }
  vector<bool> checked(N);
  long long ans = 0;
  sort((edges).begin(), (edges).end(), cmp);
  for (int(i) = (0); (i) < (edges.size()); ++(i)) {
    long long from = edges[i].from;
    long long to = edges[i].to;
    long long cost = edges[i].cost;
    if (!checked[from] || !checked[to]) {
      checked[from] = true;
      checked[to] = true;
      ans += cost;
    }
  }
  cout << ans << endl;
}
int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> x(N);
  std::vector<long long> y(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &x[i]);
    scanf("%lld", &y[i]);
  }
  solve(N, std::move(x), std::move(y));
  return 0;
}
