#include <bits/stdc++.h>
using namespace std;
int readint() {
  char c;
  while (c = getchar(), (c < '0' || c > '9') && c != '-')
    ;
  bool flag = (c == '-');
  if (flag) c = getchar();
  int x = 0;
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - 48;
    c = getchar();
  }
  return flag ? -x : x;
}
const int maxn = 100005;
int n;
long long ans;
int x[maxn], y[maxn];
set<int> from[maxn];
vector<pair<int, int> > X, Y;
vector<int> dis;
map<pair<int, int>, bool> mp;
int main() {
  scanf("%d", &n);
  int i = 1, j = 1;
  while (j <= n) {
    scanf("%d%d", &x[i], &y[i]);
    j++;
    if (mp[make_pair(x[i], y[i])]) continue;
    mp[make_pair(x[i], y[i])] = true;
    X.push_back(make_pair(x[i], i));
    Y.push_back(make_pair(y[i], i));
    i++;
  }
  n = i - 1;
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  for (int(i) = 1; (i) <= n; i++) {
    int disx = 1e9, disy = 1e9, vx, vy;
    int k1 = lower_bound(X.begin(), X.end(), make_pair(x[i], i)) - X.begin();
    int k2 = lower_bound(Y.begin(), Y.end(), make_pair(y[i], i)) - Y.begin();
    int posx_l = k1 - 1, posx_r = k1 + 1, posy_l = k2 - 1, posy_r = k2 + 1;
    while (posx_l >= 0 && from[i].count(X[posx_l].second)) posx_l--;
    while (posx_r < n && from[i].count(X[posx_r].second)) posx_r++;
    while (posy_l != 0 && from[i].count(Y[posy_l].second)) posy_l--;
    while (posy_r < n && from[i].count(Y[posy_r].second)) posy_r++;
    if (posx_l >= 0)
      if (disx > abs(X[posx_l].first - x[i])) {
        disx = abs(X[posx_l].first - x[i]);
        vx = X[posx_l].second;
      }
    if (posx_r < n)
      if (disx > abs(X[posx_r].first - x[i])) {
        disx = abs(X[posx_r].first - x[i]);
        vx = X[posx_r].second;
      }
    if (posy_l >= 0)
      if (disy > abs(Y[posy_l].first - y[i])) {
        disy = abs(Y[posy_l].first - y[i]);
        vy = Y[posy_l].second;
      }
    if (posy_r < n)
      if (disy > abs(Y[posy_r].first - y[i])) {
        disy = abs(Y[posy_r].first - y[i]);
        vy = Y[posy_r].second;
      }
    if (disx < disy) {
      dis.push_back(disx);
      from[vx].insert(i);
    } else {
      dis.push_back(disy);
      from[vy].insert(i);
    }
  }
  sort(dis.begin(), dis.end());
  for (int(i) = 0; (i) < n - 1; i++) ans += (long long)dis[i];
  printf("%lld", ans);
  return 0;
}
