#include <bits/stdc++.h>
using namespace std;
namespace utils {
template <typename T>
void print(vector<vector<T>> mat) {
  for (int i = 0; i < (int)(mat.size()); i++) {
    for (int j = 0; j < (int)(mat[0].size()); j++) cout << mat[i][j] << ' ';
    cout << endl;
  }
}
template <typename T>
void print(vector<T> v) {
  for (int i = 0; i < (int)(v.size()); i++) cout << v[i] << ' ';
  cout << endl;
}
template <typename T>
pair<T, T> shape(vector<vector<T>> mat) {
  int d1, d2;
  d1 = mat.size();
  if (d1 > 0)
    d2 = mat[0].size();
  else
    int d2 = 0;
  cout << "(" << d1 << ", " << d2 << ")" << endl;
  return make_pair(0, 0);
}
template <typename T>
vector<vector<T>> empty(int n, int m) {
  vector<vector<T>> mat(n, vector<T>(m));
  return mat;
}
}  // namespace utils
const long long INF = 1 << 28;
long long cost(pair<int, int> p1, pair<int, int> p2) {
  int a = p1.first;
  int b = p1.second;
  int c = p2.first;
  int d = p2.second;
  return min(abs(a - c), abs(b - d));
}
int main() {
  int n, x, y;
  cin >> n;
  vector<long long> mincost(n);
  vector<bool> used(n);
  vector<pair<long long, long long>> p;
  for (int i = 0; i < (int)(n); i++) {
    cin >> x >> y;
    p.push_back(make_pair(x, y));
  }
  for (int i = 0; i < n; i++) {
    mincost[i] = INF;
    used[i] = false;
  }
  mincost[0] = 0;
  long long res = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < n; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
    }
    if (v == -1) break;
    used[v] = true;
    res += mincost[v];
    for (int u = 0; u < n; u++) {
      mincost[u] = min(mincost[u], cost(p[u], p[v]));
    }
  }
  cout << res << endl;
}
