#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e12;
pair<long long, long long> x[100010];
pair<long long, long long> y[100010];
long long mn[100010];
int main() {
  long long n;
  cin >> n;
  for (long long i = (0); i < (long long)n; i++) mn[i] = inf;
  for (long long i = (0); i < (long long)n; i++) {
    cin >> x[i].first >> y[i].first;
    x[i].second = i;
    y[i].second = i;
  }
  sort(x, x + n);
  sort(y, y + n);
  for (long long i = (0); i < (long long)n - 1; i++) {
    mn[x[i].second] =
        min<long long>(mn[x[i].second], x[i + 1].first - x[i].first);
    mn[x[i + 1].second] =
        min<long long>(mn[x[i + 1].second], x[i + 1].first - x[i].first);
    mn[y[i].second] =
        min<long long>(mn[y[i].second], y[i + 1].first - y[i].first);
    mn[y[i + 1].second] =
        min<long long>(mn[y[i + 1].second], y[i + 1].first - y[i].first);
  }
  long long ans = 0;
  for (long long i = (0); i < (long long)n - 1; i++) {
    ans += mn[i];
  }
  cout << (ans) << endl;
  return 0;
}
