#include <bits/stdc++.h>
using namespace std;
unsigned long long N;
map<int, pair<int, int>> fillmap(vector<pair<int, int>> xs,
                                 map<int, pair<int, int>> &m) {
  for (auto it = xs.begin(); it + 1 != xs.end(); ++it) {
    int diff = abs(it->first - (it + 1)->first);
    if (diff <= m[it->second].first)
      m[it->second] = make_pair(diff, (it + 1)->second);
    if (diff <= m[(it + 1)->second].first)
      m[(it + 1)->second] = make_pair(diff, it->second);
  }
  return m;
}
int main(int argc, char const *argv[]) {
  cin >> N;
  vector<pair<int, int>> xs, ys;
  for (int i = 1; i <= N; ++i) {
    int x, y;
    cin >> x >> y;
    xs.push_back(make_pair(x, i));
    ys.push_back(make_pair(y, i));
  }
  sort(xs.begin(), xs.end());
  sort(ys.begin(), ys.end());
  map<int, pair<int, int>> m;
  for (int i = 1; i <= N; ++i) {
    m[i] = make_pair(2000000000, 0);
  }
  m = fillmap(xs, m);
  m = fillmap(ys, m);
  int ans = 0;
  set<pair<int, int>> visit;
  for (int i = 1; i <= N; ++i) {
    if (visit.count(make_pair(i, m[i].second))) continue;
    ans += m[i].first;
    visit.insert(make_pair(i, m[i].second));
    visit.insert(make_pair(m[i].second, i));
  }
  cout << ans << endl;
  return 0;
}
