#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > pp;
int prim(vector<vector<int> > &g) {
  int ret = 0;
  int s = g.size();
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  vector<int> v(s + 1, 0);
  q.push(pair<int, int>(0, 0));
  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    if (v[p.second]++) continue;
    ret += p.first;
    int now = p.second;
    for (int i = 0; i < g[now].size(); i++) {
      q.push(pair<int, int>(min(abs(pp[now].first - pp[g[now][i]].first),
                                abs(pp[now].second - pp[g[now][i]].second)),
                            g[now][i]));
    }
  }
  return ret;
}
int main() {
  int n;
  vector<pair<pair<int, int>, int> > g1, g2;
  vector<vector<int> > g0;
  cin >> n;
  g0.resize(n + 1);
  pp.resize(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    pp[i] = pair<int, int>(x, y);
    g1.push_back(pair<pair<int, int>, int>(pair<int, int>(x, y), i));
    g2.push_back(pair<pair<int, int>, int>(pair<int, int>(y, x), i));
  }
  sort(g1.begin(), g1.end());
  sort(g2.begin(), g2.end());
  for (int i = 0; i < n; i++) {
    g0[i].push_back(g1[((i - 1 + n) % n)].second),
        g0[i].push_back(g1[((i + 1) % n)].second);
    g0[i].push_back(g2[((i - 1 + n) % n)].second),
        g0[i].push_back(g2[((i + 1) % n)].second);
  }
  cout << prim(g0) << endl;
}
