#include <bits/stdc++.h>
using namespace std;
vector<long long int> x(1e5 + 5);
vector<long long int> y(1e5 + 5);
struct Edge {
  long long int to;
  long long int cost;
  bool operator<(const Edge &r) const { return cost > r.cost; }
};
long long int dijkstra(long long int n) {
  long long int sum = 0;
  priority_queue<Edge> que;
  long long int INF = 1e10;
  vector<long long int> memo(n, INF);
  memo[0] = INF;
  for (long long int to = 0; to < (n); to++) {
    if (to == 0) continue;
    long long int xCost = abs(x[0] - x[to]);
    long long int yCost = abs(y[0] - y[to]);
    memo[to] = min(xCost, yCost);
  }
  for (long long int times = 0; times < (n - 1); times++) {
    long long int next = 0;
    long long int cost = INF;
    for (long long int i = 0; i < (n); i++) {
      if (memo[i] < cost) {
        next = i;
        cost = memo[i];
      }
    }
    memo[next] = INF;
    sum += cost;
    for (long long int to = 0; to < (n); to++) {
      if (to == next) continue;
      if (memo[to] == INF) continue;
      long long int xCost = abs(x[next] - x[to]);
      long long int yCost = abs(y[next] - y[to]);
      memo[to] = min(memo[to], min(xCost, yCost));
    }
  }
  return sum;
}
int main() {
  long long int n;
  cin >> n;
  for (long long int i = 0; i < (n); i++) cin >> x[i] >> y[i];
  cout << dijkstra(n) << endl;
  return 0;
}
