#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1, MOD = 1e9 + 7;
struct Things {
  int cost, from, to;
  bool operator<(Things t) const { return cost < t.cost; }
};
struct Point {
  int x, y, i;
  bool operator<(Point p) const {
    if (x == p.x) {
      return y < p.y;
    }
    return x < p.x;
  }
};
Point a[N], b[N];
bool done[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d %d", &a[i].x, &a[i].y);
    a[i].i = i;
    b[i] = {a[i].y, a[i].x, i};
  }
  sort(a, a + n);
  sort(b, b + n);
  vector<Things> things;
  for (int i = 1; i < n; ++i) {
    things.push_back({abs(a[i].x - a[i - 1].x), a[i].i, a[i - 1].i});
    things.push_back({abs(b[i].x - b[i - 1].x), b[i].i, b[i - 1].i});
  }
  sort(things.begin(), things.end());
  long long result = 0;
  for (auto x : things) {
    if (!done[x.from] || !done[x.to]) {
      done[x.from] = done[x.to] = true;
      result += x.cost;
    }
  }
  printf("%lld\n", result);
  return 0;
}
