#include <bits/stdc++.h>
using namespace std;
const int maxx = 1e9;
int dis[100005], biao[100005];
struct node {
  int x, y;
} s[100005];
int main() {
  int n;
  long long int sum;
  while (scanf("%d", &n) == 1) {
    memset(dis, 0, sizeof(dis));
    memset(biao, 0, sizeof(biao));
    sum = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &s[i].x, &s[i].y);
    }
    dis[1] = 0;
    biao[1] = 1;
    for (int i = 2; i <= n; i++) {
      dis[i] = min(abs(s[i].x - s[1].x), abs(s[i].y - s[1].y));
    }
    for (int i = 2; i <= n; i++) {
      int ant = maxx;
      int jj = 0;
      for (int j = 1; j <= n; j++) {
        if (!biao[j] && dis[j] < ant) {
          ant = dis[j];
          jj = j;
        }
      }
      biao[jj] = 1;
      sum = sum + ant;
      for (int j = 1; j <= n; j++) {
        if (!biao[j] &&
            min(abs(s[j].x - s[jj].x), abs(s[j].y - s[jj].y)) < dis[j]) {
          dis[j] = min(abs(s[j].x - s[jj].x), abs(s[j].y - s[jj].y));
        }
      }
    }
    printf("%lld\n", sum);
  }
}
