#include <bits/stdc++.h>
using namespace std;
struct node {
  int x;
  int y;
  int index;
};
struct node2 {
  int l;
  pair<int, int> x;
};
node a[100005];
node b[100005];
node2 c[200005];
bool cmp1(node n, node m) { return n.x < m.x; }
bool cmp2(node n, node m) { return n.y < m.y; }
bool cmp3(node2 n, node2 m) { return n.l < m.l; }
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].x >> a[i].y;
    a[i].index = i;
    b[i].x = a[i].x;
    b[i].y = a[i].y;
    b[i].index = i;
  }
  sort(a, a + n, cmp1);
  sort(b, b + n, cmp2);
  int cur = 0;
  for (int i = 1; i < n; i++) {
    c[cur].l = a[i].x - a[i - 1].x;
    c[cur].x = make_pair(a[i].index, a[i - 1].index);
    cur++;
  }
  for (int i = 1; i < n; i++) {
    c[cur].l = b[i].y - b[i - 1].y;
    c[cur].x = make_pair(b[i].index, b[i - 1].index);
    cur++;
  }
  sort(c, c + cur, cmp3);
  long long ans = 0;
  set<pair<int, int> > s;
  int cnt = 0;
  for (int i = 0; i < cur && cnt < n - 1; i++) {
    if (s.count(c[i].x) == 0) {
      s.insert(c[i].x);
      s.insert(make_pair(c[i].x.second, c[i].x.first));
      cnt++;
      ans += c[i].l;
    }
  }
  cout << ans << "\n";
  return 0;
}
