#include <bits/stdc++.h>
using namespace std;
const int INF = 0x2aaaaaaa;
const long long INFLL = 0x1f1f1f1f1f1f1f1fLL;
template <class A, class B>
inline ostream& operator<<(ostream& st, const pair<A, B>& P) {
  return st << "(" << P.first << "," << P.second << ")";
};
template <class A, class B>
inline pair<A, B> operator+(const pair<A, B>& P, const pair<A, B>& Q) {
  return pair<A, B>(P.first + Q.first, P.second + Q.second);
};
template <class A, class B>
inline pair<A, B> operator-(const pair<A, B>& P, const pair<A, B>& Q) {
  return pair<A, B>(P.first - Q.first, P.second - Q.second);
};
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
priority_queue<pair<int, int> > Q[100000];
int xy[100000][2];
bool f[100000];
int main() {
  memset(f, 0, sizeof(f));
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> xy[i][0] >> xy[i][1];
  }
  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++) {
      long long d = min(abs(xy[i][0] - xy[j][0]), abs(xy[i][1] - xy[j][1]));
      Q[i].push({-d, j});
      Q[j].push({-d, i});
    }
  int cnt = 1;
  UnionFind U(N);
  long long ret = 0;
  for (;;) {
    for (int i = 0; i < N; i++) {
      auto t = Q[i].top();
      Q[i].pop();
      if (U.unionSet(t.second, i)) {
        cnt++;
        ret += -t.first;
        f[i] = true;
        f[t.second] = true;
        if (cnt == N) {
          cout << ret << endl;
          return 0;
          break;
        }
      }
    }
  }
}
