#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], from[maxn];
vector<pair<int, int> > X, Y;
map<pair<int, int>, bool> mp;
int main() {
  scanf("%d", &n);
  int i = 1;
  while (i <= n) {
    scanf("%d%d", &x[i], &y[i]);
    if (mp[make_pair(x[i], y[i])]) continue;
    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 - 1; 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();
    if (k1 == 0 || X[k1 - 1].second == from[i])
      disx = abs(X[k1 + 1].first - x[i]), vx = X[k1 + 1].second;
    else if (k1 == n - 1 || X[k1 + 1].second == from[i])
      disx = abs(X[k1 - 1].first - x[i]), vx = X[k1 - 1].second;
    else if (abs(X[k1 - 1].first - x[i]) < abs(X[k1 + 1].first - x[i]))
      disx = abs(X[k1 - 1].first - x[i]), vx = X[k1 - 1].second;
    else
      disx = abs(X[k1 + 1].first - x[i]), vx = X[k1 + 1].second;
    if (k2 == 0 || Y[k2 - 1].second == from[i])
      disy = abs(Y[k2 + 1].first - y[i]), vy = Y[k2 + 1].second;
    else if (k2 == n - 1 || Y[k2 + 1].second == from[i])
      disy = abs(Y[k2 - 1].first - y[i]), vy = Y[k2 - 1].second;
    else if (abs(Y[k2 - 1].first - y[i]) < abs(Y[k2 + 1].first - y[i]))
      disy = abs(Y[k2 - 1].first - y[i]), vy = Y[k2 - 1].second;
    else
      disy = abs(Y[k2 + 1].first - y[i]), vy = Y[k2 + 1].second;
    if (disx < disy) {
      ans += (long long)disx;
      from[vx] = i;
    } else {
      ans += (long long)disy;
      from[vy] = i;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
