#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<pair<int, int> > a(n);
  vector<vector<pair<int, int> > > v(n);
  vector<bool> m(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a[i] = {x, y};
  }
  sort(a.begin(), a.end());
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      v[i].push_back(
          {min(abs(a[i].first - a[j].first), abs(a[i].second - a[j].second)),
           j});
      v[j].push_back(
          {min(abs(a[j].first - a[i].first), abs(a[j].second - a[i].second)),
           i});
    }
  }
  int mn = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  q.push({0, 0});
  while (!q.empty()) {
    pair<int, int> p;
    p = q.top();
    q.pop();
    int x = p.second;
    if (m[x]) continue;
    m[x] = true;
    mn += p.first;
    for (int i = 0; i < v[x].size(); i++) {
      int y = v[x][i].second;
      if (!m[y]) q.push(v[x][i]);
    }
  }
  cout << mn;
}
