#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
pair<int, int> s[N];
int mabs(int x) { return x < 0 ? -x : x; }
int id1[N], id2[N];
bool cmp1(int i, int j) { return s[i].first < s[j].first; }
bool cmp2(int i, int j) { return s[i].second < s[j].second; }
int pa[N];
int fpa(int x) { return pa[x] == x ? x : pa[x] = fpa(pa[x]); }
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < (n); ++i) cin >> s[i].first >> s[i].second;
  for (int i = 0; i < (n); ++i) pa[i] = id1[i] = id2[i] = i;
  sort(id1, id1 + n, cmp1);
  sort(id2, id2 + n, cmp2);
  vector<pair<int, pair<int, int> > > v;
  for (int i = 0; i < (n - 1); ++i)
    v.push_back({s[id1[i + 1]].first - s[id1[i]].first, {id1[i], id1[i + 1]}});
  for (int i = 0; i < (n - 1); ++i)
    v.push_back(
        {s[id2[i + 1]].second - s[id2[i]].second, {id2[i], id2[i + 1]}});
  sort(v.begin(), v.end());
  long long ans = 0;
  for (auto x : v) {
    if (fpa(x.second.first) == fpa(x.second.second)) continue;
    ans += x.first;
    pa[x.second.first] = x.second.second;
  }
  cout << ans << endl;
  return 0;
}
