#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
using LL = long long;
const LL LINF = 1e18;
using namespace std;
class PalseError {};
class Edge {
 public:
  LL from, to, value;
  Edge(LL a, LL b, LL c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(LL a, LL b) {
    from = a;
    to = b;
  }
};
class UnionFind {
 private:
  vector<int> vec;

 public:
  void init(int N) {
    vec.resize(N);
    for (int a = 0; a < N; a++) {
      vec[a] = -1;
    }
  }
  int root(int a) {
    if (vec[a] < 0)
      return a;
    else
      return vec[a] = root(vec[a]);
  }
  bool connect(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b) return false;
    if (vec[a] > vec[b]) swap(a, b);
    vec[a] += vec[b];
    vec[b] = a;
    return true;
  }
  bool check(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b)
      return true;
    else
      return false;
  }
};
bool comp(const Edge& e1, const Edge& e2) { return e1.value < e2.value; }
LL kruskal(vector<Edge> Edges, int N) {
  sort((Edges).begin(), (Edges).end(), comp);
  UnionFind Uni;
  Uni.init(N);
  LL ans = 0LL;
  for (int a = 0; a < Edges.size(); a++) {
    Edge e = Edges.at(a);
    if (!Uni.check(e.from, e.to)) {
      Uni.connect(e.from, e.to);
      ans += e.value;
    }
  }
  return ans;
}
int main() {
  int(N);
  cin >> (N);
  vector<Edge> vec;
  vector<pair<int, int>> q;
  for (int a = 0; a < (N); ++a) {
    int(b);
    cin >> (b);
    int(c);
    cin >> (c);
    q.push_back(make_pair(b, c));
  }
  for (int a = 0; a < N - 1; a++) {
    for (int b = a + 1; b < N; b++) {
      Edge e(a, b,
             min(abs(q.at(a).first - q.at(b).first),
                 abs(q.at(a).second - q.at(b).second)));
      vec.push_back(e);
    }
  }
  cout << (kruskal(vec, N)) << endl;
  return 0;
}
