#include <bits/stdc++.h>
using namespace std;
void readline(char *s) {
  int len = 0;
  char c;
  for (c = ((char)fgetc(stdin)); c != '\n';
       s[len++] = c, c = ((char)fgetc(stdin)))
    ;
  s[len] = 0;
}
void readstr(char *s) {
  int len = 0;
  char c;
  for (c = ((char)fgetc(stdin)); c != ' ' && c != '\n';
       s[len++] = c, c = ((char)fgetc(stdin)))
    ;
  s[len] = 0;
}
void printstr(const char *s) {
  int len = strlen(s);
  for (int(i) = (0); (i) <= (len - 1); i++) fputc((s[i]), stdout);
}
void to_lower(char *s, int L, int R) {
  for (int(i) = (L); (i) <= (R); i++)
    if (((s[i]) >= 'A' && (s[i]) <= 'Z')) s[i] = s[i] - 'A' + 'a';
}
void to_upper(char *s, int L, int R) {
  for (int(i) = (L); (i) <= (R); i++)
    if (((s[i]) >= 'a' && (s[i]) <= 'z')) s[i] = s[i] - 'a' + 'A';
}
long long readll() {
  char c;
  long long ans = 0, x = 1;
  for (c = ((char)fgetc(stdin)); !((c) >= '0' && (c) <= '9') && c != '-';
       c = ((char)fgetc(stdin)))
    ;
  if (c == '-') x = -1, c = ((char)fgetc(stdin));
  for (; ((c) >= '0' && (c) <= '9');
       ans = ans * 10 + c - '0', c = ((char)fgetc(stdin)))
    ;
  return ans * x;
}
void printll(long long x) {
  int a[25], cnt = 0;
  if (x == 0) {
    fputc(('0'), stdout);
    return;
  }
  if (x < 0) fputc(('-'), stdout), x = -x;
  for (; x; a[cnt++] = x - x / 10 * 10, x /= 10)
    ;
  for (int(i) = (cnt - 1); (i) >= (0); i--) fputc((a[i] + '0'), stdout);
}
vector<pair<int, long long> > g[100005];
int n, x[100005], y[100005];
long long calc(int i, int j) { return min(abs(x[i] - x[j]), abs(y[i] - y[j])); }
vector<pair<long long, pair<int, int> > > edge;
int par[100005];
int find(int x) { return (x == par[x] ? x : par[x] = find(par[x])); }
void merge(int x, int y) {
  x = find(x), y = find(y);
  if (x != y) par[y] = x;
}
long long mst() {
  for (int(i) = (1); (i) <= (n); i++) par[i] = i;
  long long ans = 0;
  for (int(i) = (1); (i) <= (n); i++)
    for (int(j) = (0); (j) <= ((int)g[i].size() - 1); j++)
      edge.push_back(make_pair(g[i][j].second, make_pair(i, g[i][j].first)));
  sort(edge.begin(), edge.end());
  for (int(i) = (0); (i) <= ((int)edge.size() - 1); i++) {
    int x = edge[i].second.first, y = edge[i].second.second;
    if (find(x) == find(y)) continue;
    merge(x, y);
    ans += edge[i].first;
  }
  return ans;
}
int main() {
  n = ((int)readll());
  for (int(i) = (1); (i) <= (n); i++)
    x[i] = ((int)readll()), y[i] = ((int)readll());
  for (int(i) = (1); (i) <= (n); i++) {
    for (int(j) = (i + 1); (j) <= (n); j++) {
      g[i].push_back(make_pair(j, calc(i, j)));
      g[j].push_back(make_pair(i, calc(i, j)));
    }
  }
  long long ans = mst();
  printll(ans);
  return 0;
}
