#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
constexpr int MOD = 1e9 + 7;
constexpr int INF = 1e9 + 1;
constexpr ll LLINF = 4 * 1e18 + 1;
template <typename T>
T debug(T x) {
  cout << "x"
       << ": " << x << "\n";
  return x;
}
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
using Road = pair<int, pii>;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  ll ans = 0;
  cin >> n;
  vector<pii> towns(n);
  UnionFind uf(n);
  priority_queue<Road, vector<Road>, greater<Road>> roads;
  for (int i = 0; i < (n); ++i) cin >> towns[i].first >> towns[i].second;
  for (int i = 0; i < (n); ++i) {
    for (int j = 0; j < (i); ++j) {
      roads.push(make_pair((min(abs(towns[i].first - towns[j].first),
                                abs(towns[i].second - towns[j].second))),
                           (make_pair((j), (i)))));
    }
  }
  while (!roads.empty()) {
    auto r = roads.top();
    roads.pop();
    if (uf.findSet(r.second.first, r.second.second)) continue;
    uf.unionSet(r.second.first, r.second.second);
    ans += r.first;
  }
  cout << ans << "\n";
  return 0;
}
