#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll Mod = 1000000007;
ll INF = Mod * 100LL;
int main() {
  ll N;
  cin >> N;
  ll x[N], y[N];
  multiset<pair<ll, ll> > SetX;
  multiset<pair<ll, ll> > SetY;
  vector<int> Graph[N];
  vector<ll> vec;
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i];
    SetX.insert(make_pair(x[i], i));
    SetY.insert(make_pair(y[i], i));
  }
  for (int i = 0; i < N; i++) {
    ll res = INF;
    ll num = -1;
    auto itr1 = SetX.lower_bound(make_pair(x[i], -1));
    SetX.erase(itr1);
    auto itr2 = SetY.lower_bound(make_pair(y[i], -1));
    SetY.erase(itr2);
    for (int j = 0; j < (int)Graph[i].size(); j++) {
      int s = Graph[i][j];
      auto itrx = SetX.lower_bound(make_pair(x[s], -1));
      auto itry = SetY.lower_bound(make_pair(y[s], -1));
      SetX.erase(itrx);
      SetY.erase(itry);
    }
    auto itr3 = SetX.lower_bound(make_pair(x[i], -1));
    if (itr3 != SetX.end()) {
      if (res > abs(x[i] - (*itr3).first)) {
        res = abs(x[i] - (*itr3).first);
        num = (*itr3).second;
      }
    }
    if (itr3 != SetX.begin()) {
      auto pre1 = prev(itr3, 1);
      if (res > abs(x[i] - (*pre1).first)) {
        res = abs(x[i] - (*pre1).first);
        num = (*pre1).second;
      }
    }
    auto itr4 = SetY.lower_bound(make_pair(y[i], -1));
    if (itr4 != SetY.end()) {
      if (res > abs(y[i] - (*itr4).first)) {
        res = abs(y[i] - (*itr4).first);
        num = (*itr4).second;
      }
    }
    if (itr4 != SetY.begin()) {
      auto pre2 = prev(itr4, 1);
      if (res > abs(y[i] - (*pre2).first)) {
        res = abs(y[i] - (*pre2).first);
        num = (*pre2).second;
      }
    }
    vec.push_back(res);
    SetX.insert(make_pair(x[i], i));
    SetY.insert(make_pair(y[i], i));
    for (int j = 0; j < (int)Graph[i].size(); j++) {
      int s = Graph[i][j];
      SetX.insert(make_pair(x[s], s));
      SetY.insert(make_pair(y[s], s));
    }
    Graph[i].push_back(num);
    Graph[num].push_back(i);
  }
  sort(vec.begin(), vec.end());
  ll ans = 0;
  for (int i = 0; i < (int)vec.size() - 1; i++) {
    ans += vec[i];
  }
  cout << ans << endl;
  return 0;
}
