#include <bits/stdc++.h>
using namespace std;
int n;
long long ans;
priority_queue<pair<int, pair<int, int> > > pq;
pair<pair<int, int>, int> g[100005];
set<int> t;
int cnt;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> g[i].first.first >> g[i].first.second;
    g[i].second = i;
  }
  sort(g, g + n);
  for (int i = 0; i < n - 1; i++) {
    int x1 = g[i].first.first, y1 = g[i].first.second, id1 = g[i].second;
    int x2 = g[i + 1].first.first, y2 = g[i + 1].first.second,
        id2 = g[i + 1].second;
    pq.push(make_pair(-min(abs(x1 - x2), abs(y1 - y2)), make_pair(id1, id2)));
    g[i].first.first = y1;
    g[i].first.second = x1;
  }
  int x = g[n - 1].first.first, y = g[n - 1].first.second;
  g[n - 1].first.first = y;
  g[n - 1].first.second = x;
  sort(g, g + n);
  for (int i = 0; i < n - 1; i++) {
    int x1 = g[i].first.first, y1 = g[i].first.second, id1 = g[i].second;
    int x2 = g[i + 1].first.first, y2 = g[i + 1].first.second,
        id2 = g[i + 1].second;
    pq.push(make_pair(-min(abs(x1 - x2), abs(y1 - y2)), make_pair(id1, id2)));
  }
  while (cnt < n - 1) {
    int id1 = pq.top().second.first, id2 = pq.top().second.second,
        len = -pq.top().first;
    pq.pop();
    if (!((t.find(id1) != t.end()) && (t.find(id2) != t.end()))) {
      ans += ((long long)len);
      t.insert(id1);
      t.insert(id2);
      cnt++;
    }
  }
  cout << ans << endl;
  return 0;
}
