#include <bits/stdc++.h>
const int inf = INT_MAX;
const int nmax = 1e5 + 5;
const int mod = 1e9 + 7;
using namespace std;
int n, i, j, viz[nmax], pos1[nmax], pos2[nmax], idx, nr, a[nmax], b[nmax];
long long ans, mn;
pair<long long, int> x[nmax];
pair<long long, int> y[nmax];
vector<int> vec[nmax];
void dfs(int arr[nmax], int x) {
  viz[x] = arr[x] = 1;
  for (int i = 0; i < vec[x].size(); i++)
    if (!viz[vec[x][i]]) dfs(arr, vec[x][i]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x[i].first >> y[i].first;
    x[i].second = y[i].second = i;
  }
  sort(x + 1, x + n + 1);
  sort(y + 1, y + n + 1);
  for (i = 1; i <= n; i++) pos1[x[i].second] = pos2[y[i].second] = i;
  for (i = 1; i <= n; i++)
    if (!viz[i]) {
      mn = inf;
      if (pos1[i] > 1 && mn > abs(x[pos1[i] - 1].first - x[pos1[i]].first)) {
        mn = abs(x[pos1[i] - 1].first - x[pos1[i]].first);
        idx = x[pos1[i] - 1].second;
      }
      if (pos1[i] < n && mn > abs(x[pos1[i] + 1].first - x[pos1[i]].first)) {
        mn = abs(x[pos1[i] + 1].first - x[pos1[i]].first);
        idx = x[pos1[i] + 1].second;
      }
      if (pos2[i] > 1 && mn > abs(y[pos2[i] - 1].first - y[pos2[i]].first)) {
        mn = abs(y[pos2[i] - 1].first - y[pos2[i]].first);
        idx = y[pos2[i] - 1].second;
      }
      if (pos2[i] < n && mn > abs(y[pos2[i] + 1].first - y[pos2[i]].first)) {
        mn = abs(y[pos2[i] + 1].first - y[pos2[i]].first);
        idx = y[pos2[i] + 1].second;
      }
      viz[i] = viz[idx] = 1, ans += mn;
      vec[i].push_back(idx);
      vec[idx].push_back(i);
    }
  fill(viz, viz + n + 1, 0);
  for (i = 1; i <= n; i++)
    if (!viz[i]) {
      nr++;
      if (nr == 1)
        dfs(a, i);
      else if (nr == 2)
        dfs(b, i);
    }
  if (nr == 1)
    cout << ans << endl;
  else {
    mn = inf;
    for (i = 2; i < n; i++) {
      if ((a[x[i].second] && b[x[i - 1].second]) ||
          (b[x[i].second] && a[x[i - 1].second]))
        mn = min(mn, abs(x[i].first - x[i - 1].first));
      if ((a[x[i].second] && b[x[i + 1].second]) ||
          (b[x[i].second] && a[x[i + 1].second]))
        mn = min(mn, abs(x[i].first - x[i + 1].first));
      if ((a[y[i].second] && b[y[i - 1].second]) ||
          (b[y[i].second] && a[y[i - 1].second]))
        mn = min(mn, abs(y[i].first - y[i - 1].first));
      if ((a[y[i].second] && b[y[i + 1].second]) ||
          (b[y[i].second] && a[y[i + 1].second]))
        mn = min(mn, abs(y[i].first - y[i + 1].first));
    }
    cout << ans + mn << endl;
  }
  return 0;
}
