#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000;
const int WHITE = 0;
const int BLACK = 1;
const int GRAY = 2;
const int INFTY = (int)1 << 21;
int n;
int cost_func(pair<int, int> a, pair<int, int> b) {
  int A = abs(a.first - b.first);
  int B = abs(a.second - b.second);
  return min(A, B);
}
int prim(vector<vector<int> > list) {
  vector<int> color(n, WHITE);
  vector<int> d(n, INFTY);
  vector<int> p(n, -1);
  d[0] = 0;
  while (true) {
    int mincost = INFTY;
    int u = -1;
    for (int i = 0; i < n; i++) {
      if (color[i] != BLACK && d[i] < mincost) {
        mincost = d[i];
        u = i;
      }
    }
    if (u == -1) break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK) {
        if (d[v] > list[u][v]) {
          d[v] = list[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }
  int total = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) {
      total += list[i][p[i]];
    }
  }
  return total;
}
int main(void) {
  cin >> n;
  vector<pair<int, int> > pos(n);
  vector<vector<int> > list(n, vector<int>(n, 0));
  int x, y;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    pos[i] = make_pair(x, y);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int tmp = cost_func(pos[i], pos[j]);
      list[i][j] = tmp;
    }
  }
  cout << prim(list) << endl;
  return 0;
}
