#include <bits/stdc++.h>
using namespace std;
int n;
struct point {
  int x;
  int y;
  int idx;
} p[100004];
bool cmp1(point a, point b) { return a.x < b.x; }
bool cmp2(point a, point b) { return a.y < b.y; }
pair<int, pair<int, int>> e[200004];
int parent[100004];
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i].x >> p[i].y;
    p[i].idx = i;
  }
  sort(p + 1, p + n + 1, cmp1);
  for (int i = 1; i < n; i++) {
    e[i].first = abs(p[i].x - p[i + 1].x);
    e[i].second = {p[i].idx, p[i + 1].idx};
  }
  sort(p + 1, p + n + 1, cmp2);
  for (int i = 0; i < n - 1; i++) {
    e[i + n].first = abs(p[i + 1].y - p[i + 2].y);
    e[i + n].second = {p[i + 1].idx, p[i + 2].idx};
  }
  for (int i = 1; i <= n; i++) parent[i] = i;
  sort(e + 1, e + 2 * n - 1);
  long long ans = 0;
  for (int i = 1; i <= 2 * n - 2; i++) {
    int a = e[i].second.first;
    int b = e[i].second.second;
    if (parent[a] == 0 && parent[b] == 0) continue;
    parent[a] = 0, parent[b] = 0;
    ans += e[i].first;
  }
  cout << ans;
  return 0;
}
