#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > arr;
struct node {
  int num, data;
  node() {}
  node(int x, int y) {
    num = x;
    data = y;
  }
};
bool operator<(node a, node b) { return a.data > b.data; }
priority_queue<node> que;
bool vis[100500];
int dis[100500];
int getdis(int x, int y) {
  return min(abs(arr[x].first - arr[y].first),
             abs(arr[x].second - arr[y].second));
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    arr.push_back(pair<int, int>(x, y));
  }
  sort(arr.begin(), arr.end());
  arr.erase(unique(arr.begin(), arr.end()), arr.end());
  n = arr.size();
  for (int i = 1; i < n; i++) dis[i] = 0x7fffffff;
  dis[0] = 0;
  que.push(node(0, 0));
  long long res = 0;
  while (!que.empty()) {
    int x = que.top().num;
    if (vis[x]) {
      que.pop();
      continue;
    }
    vis[x] = true;
    res = res + que.top().data;
    que.pop();
    for (int i = 0; i < n; i++)
      if (!vis[i]) {
        int temp = getdis(x, i);
        if (temp < dis[i]) dis[i] = temp, que.push(node(i, temp));
      }
  }
  printf("%d\n", res);
  return 0;
}
