#include <bits/stdc++.h>
struct edge {
  long long int u, v, cost;
};
bool comp(const edge& e1, const edge& e2) { return e1.cost < e2.cost; }
class Union_Find {
  std::vector<long long int> per;
  std::vector<long long int> rank;

 public:
  void init(long long int size) {
    for (int i = 0; i < size; ++i) {
      per.push_back(i);
      rank.push_back(0);
    }
  }
  long long int get_per(long long int x) {
    if (x == per[x]) return x;
    return per[x] = get_per(per[x]);
  }
  bool is_same_per(long long int x, long long int y) {
    return get_per(x) == get_per(y);
  }
  void togett(long long int x, long long int y) {
    long long int x_per = get_per(x), y_per = get_per(y);
    if (x_per == y_per) return;
    if (rank[x_per] < rank[y_per])
      per[x_per] = y_per;
    else {
      per[y_per] = x_per;
      if (rank[x_per] == rank[y_per]) ++rank[x_per];
    }
  }
};
const long long int inf = 1123456789012345;
long long int N;
std::vector<std::pair<long long int, long long int>> xp, yp;
std::vector<edge> edges;
Union_Find tree;
long long int kruskal() {
  std::sort(edges.begin(), edges.end(), comp);
  long long int res = 0;
  for (auto e : edges) {
    if (!tree.is_same_per(e.u, e.v)) {
      tree.togett(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}
int main() {
  std::cin >> N;
  std::vector<long long int> x(N), y(N);
  tree.init(N + 1);
  for (int i = 0; i < N; ++i) {
    std::cin >> x[i] >> y[i];
    xp.push_back(std::make_pair(x[i], i + 1));
    yp.push_back(std::make_pair(y[i], i + 1));
  }
  std::sort(xp.begin(), xp.end());
  std::sort(yp.begin(), yp.end());
  for (long long int i = 1; i < N - 1; ++i) {
    edges.push_back(
        {xp[i - 1].second, xp[i].second, xp[i].first - xp[i - 1].first});
    edges.push_back(
        {xp[i + 1].second, xp[i].second, xp[i + 1].first - xp[i].first});
    edges.push_back(
        {yp[i - 1].second, yp[i].second, yp[i].first - yp[i - 1].first});
    edges.push_back(
        {yp[i + 1].second, yp[i].second, yp[i + 1].first - yp[i].first});
  }
  std::cout << kruskal() << std::endl;
  return 0;
}
