#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int cost;
  int from;
  int to;
  bool operator<(const Edge& another) const { return cost < another.cost; }
};
vector<Edge> edges;
pair<int, pair<int, int> > points[100001];
bool connected[100001];
bool cmp_x(pair<int, pair<int, int> > p1, pair<int, pair<int, int> > p2) {
  return p1.second.first < p2.second.first;
}
bool cmp_y(pair<int, pair<int, int> > p1, pair<int, pair<int, int> > p2) {
  return p1.second.second < p2.second.second;
}
int main(void) {
  int N, x, y;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d%d", &x, &y);
    connected[i] = false;
    points[i] = make_pair(i, make_pair(x, y));
  }
  sort(points, points + N, cmp_x);
  for (int i = 1; i < N; i++) {
    pair<int, pair<int, int> > p1 = points[i], p2 = points[i - 1];
    edges.push_back(
        Edge{p1.second.first - p2.second.first, p1.first, p2.first});
  }
  sort(points, points + N, cmp_y);
  for (int i = 1; i < N; i++) {
    pair<int, pair<int, int> > p1 = points[i], p2 = points[i - 1];
    edges.push_back(
        Edge{p1.second.second - p2.second.second, p1.first, p2.first});
  }
  sort(edges.begin(), edges.end());
  long long ans = 0;
  int num = 0;
  for (int i = 0; i < edges.size(); i++) {
    Edge e = edges[i];
    int from = e.from, to = e.to;
    if (connected[from] && connected[to]) continue;
    ans += e.cost;
    if (!connected[from]) {
      connected[from] = true;
      num++;
    }
    if (!connected[to]) {
      connected[to] = true;
      num++;
    }
    if (num == N) break;
  }
  return !printf("%lld\n", ans);
}
