#include <bits/stdc++.h>
using namespace std;
struct point {
  int id, x, y;
};
struct edge {
  int u, v, val;
  edge(int u, int v, int val) : u(u), v(v), val(val) {}
  bool operator>(const edge& e) const { return val > e.val; }
};
vector<point> points;
priority_queue<edge, vector<edge>, greater<edge> > E;
vector<int> far;
int mabs(int x, int y) { return x > y ? x - y : y - x; }
int find(int x) { return far[x] < 0 ? x : (far[x] = find(far[x])); }
void merge(int x, int y) {
  x = find(x);
  y = find(y);
  if ((-far[x]) < (-far[y])) {
    far[y] += far[x];
    far[x] = y;
  } else {
    far[x] += far[y];
    far[y] = x;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int x, y;
  for (int i = 0; i < n; ++i) {
    point p;
    cin >> p.x >> p.y;
    p.id = i;
    points.push_back(p);
  }
  sort(points.begin(), points.end(),
       [](const point& a, const point& b) -> bool { return a.x < b.x; });
  for (int i = 0; i + 1 < points.size(); ++i) {
    int u = points[i].id;
    int v = points[i + 1].id;
    int val = mabs(points[i].x, points[i + 1].x);
    E.push(edge(u, v, val));
  }
  sort(points.begin(), points.end(),
       [](const point& a, const point& b) -> bool { return a.y < b.y; });
  for (int i = 0; i + 1 < points.size(); ++i) {
    int u = points[i].id;
    int v = points[i + 1].id;
    int val = mabs(points[i].y, points[i + 1].y);
    edge e = edge(u, v, val);
    E.push(e);
  }
  far = vector<int>(n, -1);
  int count = 0;
  long long cost = 0;
  while (count < n - 1) {
    const edge e = E.top();
    E.pop();
    if (find(e.u) > 0 && find(e.v) > 0 && find(e.u) == find(e.v))
      continue;
    else {
      merge(e.u, e.v);
      cost += e.val;
      ++count;
    }
  }
  cout << cost << endl;
  return 0;
}
