#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 0x7ffffff;
struct node {
  int first, second;
  int num;
} a[100048];
struct node2 {
  int d;
  bool s;
  int num;
  friend bool operator<(const node2 &first, const node2 &second) {
    return first.d > second.d;
  }
};
int n;
map<int, bool> m;
map<int, int> rhao, chao;
int rt = 0, ct = 0;
vector<pair<int, int> > vr[100048], vc[100048];
priority_queue<node2> qr, qc;
pair<int, int> r[100048], c[100048];
bool cmp1(node first, node second) { return first.first < second.first; }
bool cmp2(node first, node second) { return first.second < second.second; }
bool cmp3(node first, node second) { return first.num < second.num; }
int main() {
  int i;
  cin >> n;
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].first, &a[i].second);
    if (rhao[a[i].first])
      vr[rhao[a[i].first]].push_back(make_pair(a[i].second, i));
    else {
      rhao[a[i].first] = ++rt;
      vr[rt].push_back(make_pair(a[i].second, i));
    }
    if (chao[a[i].second])
      vc[chao[a[i].second]].push_back(make_pair(a[i].first, i));
    else {
      chao[a[i].second] = ++ct;
      vc[ct].push_back(make_pair(a[i].first, i));
    }
    a[i].num = i;
  }
  sort(a + 1, a + n + 1, cmp1);
  r[a[1].num].first = -1;
  r[a[1].num].second = a[2].first - a[1].first;
  r[a[n].num].first = a[n].first - a[n - 1].first;
  r[a[n].num].second = -1;
  for (i = 2; i <= n; i++)
    r[a[i].num] =
        make_pair(a[i].first - a[i - 1].first, a[i + 1].first - a[i].first);
  sort(a + 1, a + n + 1, cmp2);
  c[a[1].num] = make_pair(-1, a[2].second - a[1].second);
  c[a[n].num] = make_pair(a[n].second - a[n - 1].second, -1);
  for (i = 2; i <= n; i++)
    c[a[i].num] =
        make_pair(a[i].second - a[i - 1].second, a[i + 1].second - a[i].second);
  node2 ins;
  ins.num = a[1].num;
  if (r[a[1].num].first != -1) {
    ins.s = true;
    ins.d = r[a[1].num].first;
    qr.push(ins);
  }
  if (r[a[1].num].second != -1) {
    ins.s = false;
    ins.d = r[a[1].num].second;
    qr.push(ins);
  }
  if (c[a[1].num].first != -1) {
    ins.s = true;
    ins.d = c[a[1].num].first;
    qc.push(ins);
  }
  if (c[a[1].num].second != -1) {
    ins.s = false;
    ins.d = c[a[1].num].second;
    qc.push(ins);
  }
  m[a[1].num] = true;
  long long ans = 0;
  int xx, yy, dd;
  node2 res;
  sort(a + 1, a + n + 1, cmp3);
  for (i = 1; i <= n - 1; i++) {
    do {
      res = qr.top();
      xx = res.s ? a[res.num].first - res.d : a[res.num].first + res.d;
      while (vr[rhao[xx]].size() > 0 && m[vr[rhao[xx]].back().second])
        vr[rhao[xx]].pop_back();
      if (vr[rhao[xx]].size() == 0) qr.pop();
    } while (vr[rhao[xx]].size() == 0);
    do {
      res = qc.top();
      yy = res.s ? a[res.num].second - res.d : a[res.num].second + res.d;
      while (vc[chao[yy]].size() > 0 && m[vc[chao[yy]].back().second])
        vc[chao[yy]].pop_back();
      if (vc[chao[yy]].size() == 0) qc.pop();
    } while (vc[chao[yy]].size() == 0);
    if (qr.top().d < qc.top().d) {
      res = qr.top();
      qr.pop();
      xx = res.s ? a[res.num].first - res.d : a[res.num].first + res.d;
      ans += res.d;
      m[vr[rhao[xx]].back().second] = true;
      dd = vr[rhao[xx]].back().second;
      vr[rhao[xx]].pop_back();
      ins.num = dd;
      if (r[dd].first != -1) {
        ins.s = true;
        ins.d = r[dd].first;
        qr.push(ins);
      }
      if (r[dd].second != -1) {
        ins.s = false;
        ins.d = r[dd].second;
        qr.push(ins);
      }
      if (c[dd].first != -1) {
        ins.s = true;
        ins.d = c[dd].first;
        qc.push(ins);
      }
      if (c[dd].second != -1) {
        ins.s = false;
        ins.d = c[dd].second;
        qc.push(ins);
      }
    } else {
      res = qc.top();
      qc.pop();
      yy = res.s ? a[res.num].second - res.d : a[res.num].second + res.d;
      ans += res.d;
      m[vc[chao[yy]].back().second] = true;
      dd = vc[chao[yy]].back().second;
      vc[chao[yy]].pop_back();
      ins.num = dd;
      if (r[dd].first != -1) {
        ins.s = true;
        ins.d = r[dd].first;
        qr.push(ins);
      }
      if (r[dd].second != -1) {
        ins.s = false;
        ins.d = r[dd].second;
        qr.push(ins);
      }
      if (c[dd].first != -1) {
        ins.s = true;
        ins.d = c[dd].first;
        qc.push(ins);
      }
      if (c[dd].second != -1) {
        ins.s = false;
        ins.d = c[dd].second;
        qc.push(ins);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
