#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
struct UnionFind {
  vector<int> par, cnt;
  UnionFind(int NV) {
    par.clear();
    cnt.resize(NV, 1);
    for (int i = 0; i < NV; i++) par.push_back(i);
  }
  void reset() {
    for (int i = 0; i < cnt.size(); i++) cnt[i] = 1;
    for (int i = 0; i < par.size(); i++) par[i] = i;
  }
  int operator[](int x) {
    return par[x] == x ? x : par[x] = operator[](par[x]);
  }
  void operator()(int x, int y) {
    x = operator[](x);
    y = operator[](y);
    if (x != y) par[x] = y, cnt[y] += cnt[x];
  }
};
int N;
void _main() {
  cin >> N;
  vector<pair<int, int>> va, vb;
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    va.push_back({x, i});
    vb.push_back({y, i});
  }
  vector<tuple<int, int, int>> edges;
  sort(va.begin(), va.end());
  sort(vb.begin(), vb.end());
  for (int i = 0; i < N - 1; i++) {
    edges.push_back(
        {va[i + 1].first - va[i].first, va[i].second, va[i + 1].second});
    edges.push_back(
        {vb[i + 1].first - vb[i].first, vb[i].second, vb[i + 1].second});
  }
  sort(edges.begin(), edges.end());
  UnionFind uf(N);
  long long ans = 0;
  for (auto p : edges) {
    int x, y, c;
    tie(c, x, y) = p;
    if (uf[x] != uf[y]) {
      uf(x, y);
      ans += c;
    }
  }
  cout << ans << endl;
}
