#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<long long, pair<int, int> > > Q;
int xy[100000][2];
int main() {
  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.push({-d, {i, j}});
    }
  int cnt = 1;
  UnionFind U(N);
  long long ret = 0;
  for (;;) {
    auto t = Q.top();
    Q.pop();
    if (U.unionSet(t.second.first, t.second.second)) {
      cnt++;
      ret += -t.first;
      if (cnt == N) break;
    }
  }
  cout << ret << endl;
}
