#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
pair<int, int> point[maxn];
bool visited[maxn];
int dist[maxn];
inline int cost(const int a, const int b) {
  return min(abs(point[a].first - point[b].first),
             abs(point[a].second - point[b].second));
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; i++)
      scanf("%d %d", &point[i].first, &point[i].second);
    memset(visited, 0, sizeof(visited));
    memset(dist, 0x3f, sizeof(dist));
    visited[0] = 1;
    dist[0] = 0;
    long long ans = 0;
    int p = 0;
    for (int i = 1; i < n; i++) {
      int idx, temp = 0x3f3f3f3f;
      for (int j = 1; j < n; j++) {
        if (visited[j]) continue;
        dist[j] = min(dist[j], cost(p, j));
        if (temp > dist[j]) {
          temp = dist[j];
          idx = j;
        }
      }
      p = idx;
      visited[p] = 1;
      ans += temp;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
