#include <bits/stdc++.h>
using namespace std;
set<pair<int, int>> psx;
set<pair<int, int>> psy;
int n;
const int N = 100001;
int x[N];
int y[N];
int dist(pair<int, int> p1, pair<int, int> p2) {
  return min(abs(p1.first - p2.first), abs(p1.second - p2.second));
}
pair<int, int> find_closest(pair<int, int> p) {
  auto it1 = psx.lower_bound({p.first, -2000000000});
  auto it2 = it1;
  if (it2 != psx.begin()) it2--;
  auto it3 = psy.lower_bound({p.second, -2000000000});
  auto it4 = it3;
  if (it4 != psy.begin()) it4--;
  pair<int, int> result = *it2;
  if (it1 != psx.end() && dist(*it1, p) < dist(result, p)) result = *it1;
  if (it3 != psy.end() && dist({it3->second, it3->first}, p) < dist(result, p))
    result = {it3->second, it3->first};
  if (dist({it4->second, it4->first}, p) < dist(result, p))
    result = {it4->second, it4->first};
  return result;
}
void remove_point(pair<int, int>& p) {
  psx.erase(p);
  psy.erase({p.second, p.first});
}
signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", x + i, y + i);
    if (i) {
      psx.insert({x[i], y[i]});
      psy.insert({y[i], x[i]});
    }
  }
  pair<int, int> ps[4];
  ps[0] = ps[1] = ps[2] = ps[3] = {x[0], y[0]};
  long long total_distance = 0;
  for (int i = 1; i < n; i++) {
    pair<int, int> closest;
    int min_dist = 2000000000;
    for (int j = 0; j < 4; j++) {
      pair<int, int> candidate = find_closest(ps[j]);
      int candidate_distance = dist(candidate, ps[j]);
      if (candidate_distance < min_dist) {
        min_dist = candidate_distance;
        closest = candidate;
      }
    }
    remove_point(closest);
    total_distance += min_dist;
    if (closest.first < ps[0].first) ps[0] = closest;
    if (closest.first > ps[1].first) ps[1] = closest;
    if (closest.second < ps[2].second) ps[2] = closest;
    if (closest.second > ps[3].second) ps[3] = closest;
  }
  printf("%lld", total_distance);
}
