#include <bits/stdc++.h>
using namespace std;
const int maxlen = 200000;
pair<long long, long long> p[maxlen];
long long cost[maxlen];
long long visited[maxlen];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first;
    cin >> p[i].second;
    cost[i] = min(abs(p[i].first - p[0].first), abs(p[i].second - p[0].second));
    visited[i] = 0;
  }
  int res = 0;
  int cnt = 1;
  visited[0] = 1;
  while (true) {
    if (cnt == n) break;
    int index = -1;
    int v = INT32_MAX;
    for (int i = 0; i < n; i++) {
      if (visited[i] == 0 && cost[i] < v) {
        v = cost[i];
        index = i;
      }
    }
    if (index == -1) break;
    cnt++;
    visited[index] = 1;
    res += cost[index];
    for (int i = 0; i < n; i++) {
      if (visited[i] == 0)
        cost[i] = min(cost[i], min(abs(p[i].first - p[index].first),
                                   abs(p[i].second - p[index].second)));
    }
  }
  cout << res << endl;
}
