#include <bits/stdc++.h>
int v[100005], dis[100005];
using namespace std;
pair<int, int> d[100005];
int prim(int n) {
  memset(v, 0, sizeof(v));
  for (int i = 1; i <= n; i++) dis[i] = 1000000009;
  dis[1] = 0;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int mark = -1;
    for (int j = 1; j <= n; j++) {
      if (!v[j]) {
        if (mark == -1)
          mark = j;
        else if (dis[j] < dis[mark])
          mark = j;
      }
    }
    if (mark == -1) break;
    v[mark] = 1;
    ans += dis[mark];
    for (int j = 1; j <= n; j++) {
      if (!v[j])
        dis[j] = min(dis[j], min(abs(d[mark].first - d[j].first),
                                 abs(d[mark].second - d[j].second)));
    }
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    d[i].first = a;
    d[i].second = b;
  }
  int ans = prim(n);
  cout << ans;
  return 0;
}
