#include <bits/stdc++.h>
using namespace std;
const long long int inf = (1LL << 60);
const long long int mod = 1000000007;
long long int max(long long int a, long long int b) {
  if (a < b)
    return b;
  else
    return a;
}
long long int min(long long int a, long long int b) {
  if (a < b)
    return a;
  else
    return b;
}
double max(double a, double b) {
  if (a < b) return b;
  return a;
}
long long int N;
priority_queue<pair<long long int, pair<int, int>>> q;
vector<pair<pair<int, int>, long long int>> d;
bool compare_b(pair<pair<int, int>, long long int> a,
               pair<pair<int, int>, long long int> b) {
  if (a.first.second > b.first.second) return true;
  return false;
}
long long int vis[100005];
int main() {
  cin >> N;
  d.resize(N);
  for (int i = 0; i < (N); i++) {
    cin >> d[i].first.first >> d[i].first.second;
    d[i].second = i;
  }
  sort(d.begin(), d.end());
  for (int i = 0; i < (d.size() - 1); i++) {
    q.push({-abs(d[i].first.first - d[i + 1].first.first),
            {d[i].second, d[i + 1].second}});
  }
  sort(d.begin(), d.end(), compare_b);
  reverse(d.begin(), d.end());
  for (int i = 0; i < (d.size() - 1); i++) {
    q.push({-abs(d[i].first.second - d[i + 1].first.second),
            {d[i].second, d[i + 1].second}});
  }
  long long int honsu = 0;
  long long int sum = 0;
  while (honsu != N - 1) {
    while (vis[q.top().second.first] == 1 && vis[q.top().second.second] == 1) {
      q.pop();
    }
    sum += -q.top().first;
    honsu += 1;
    vis[q.top().second.first] = 1;
    vis[q.top().second.second] = 1;
    q.pop();
  }
  cout << sum << endl;
  return 0;
}
