#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  int N;
  scanf("%d", &N);
  getchar();
  ;
  vector<pair<int, int>> V;
  V.reserve(N);
  for (int i = 0; i < N; ++i) {
    int xi;
    scanf("%d", &xi);
    getchar();
    ;
    int yi;
    scanf("%d", &yi);
    getchar();
    ;
    V.emplace_back(xi, yi);
  }
  auto dist = [&](int xn, int yn) {
    return min(abs(V[xn].first - V[yn].first),
               abs(V[xn].second - V[yn].second));
  };
  ll result = 0;
  unordered_set<int> used;
  multimap<int, int> passes;
  passes.emplace(0, 0);
  while (used.size() < N) {
    int len = passes.begin()->first;
    int c = passes.begin()->second;
    passes.erase(passes.begin());
    if (!used.insert(c).second) continue;
    result += len;
    for (int i = 0; i < N; ++i) {
      if (used.find(i) == used.end()) {
        passes.emplace(dist(i, c), i);
      }
    }
  }
  printf("%lld\n", result);
}
