#include <bits/stdc++.h>
using namespace std;
inline int getc() {
  static const int L = 1 << 15;
  static char buf[L], *S = buf, *T = buf;
  if (S == T) {
    T = (S = buf) + fread(buf, 1, L, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
inline int getint() {
  int c;
  while (!isdigit(c = getc()) && c != '-')
    ;
  bool sign = c == '-';
  int tmp = sign ? 0 : c - '0';
  while (isdigit(c = getc())) tmp = (tmp << 1) + (tmp << 3) + c - '0';
  return sign ? -tmp : tmp;
}
int n;
int x[100050], y[100050];
struct Node {
  int x, y, lab;
  Node(int _x = 0, int _y = 0, int _lab = 0) : x(_x), y(_y), lab(_lab) {}
  bool operator<(const Node &B) const {
    return (x > B.x || (x == B.x && y > B.y));
  }
} S[100050];
struct Edge {
  int from, to, len;
  Edge(int _from = 0, int _to = 0, int _len = 0)
      : from(_from), to(_to), len(_len) {}
  bool operator<(const Edge &B) const { return len < B.len; }
} E[100050 << 2];
int top;
int sa[100050], rank1[100050], change[100050];
inline bool cmp(const int &a, const int &b) {
  return S[a].y - S[a].x > S[b].y - S[b].x;
}
int A[100050];
int ask(int p) {
  int res = 0;
  for (; p; p -= p & -p)
    if (S[change[res]].x + S[change[res]].y >
        S[change[A[p]]].x + S[change[A[p]]].y)
      res = A[p];
  return res;
}
void modify(int p, int ins) {
  for (; p <= n; p += p & -p)
    if (S[change[ins]].x + S[change[ins]].y <
        S[change[A[p]]].x + S[change[A[p]]].y)
      A[p] = ins;
}
void AddEdge() {
  int i, j, k;
  for (i = 1; i <= n; ++i) sa[i] = i;
  sort(sa + 1, sa + n + 1, cmp);
  int id = 0;
  for (i = 1; i <= n;) {
    for (j = i;
         S[sa[j]].y - S[sa[j]].x == S[sa[j + 1]].y - S[sa[j + 1]].x && j < n;
         ++j)
      ;
    ++id;
    for (k = i; k <= j; ++k) rank1[sa[k]] = id;
    i = j + 1;
  }
  sort(S + 1, S + n + 1);
  for (i = 1; i <= n; ++i) change[S[i].lab] = i;
  memset(A, 0, sizeof(A));
  for (i = 1; i <= n; ++i) {
    int get = ask(rank1[S[i].lab]);
    if (get != 0)
      E[++top] =
          Edge(S[i].lab, get,
               min(abs(x[S[i].lab] - x[get]), abs(y[S[i].lab] - y[get])));
    modify(rank1[S[i].lab], S[i].lab);
  }
}
int root[100050];
void reset() {
  for (int i = 1; i <= n; ++i) root[i] = i;
}
int find(int x) {
  int q = x, tmpq;
  for (; x != root[x]; x = root[x])
    ;
  for (; q != x; q = tmpq) tmpq = root[q], root[q] = x;
  return x;
}
long long Kruskal() {
  sort(E + 1, E + top + 1);
  reset();
  int i;
  int ra, rb, intree = 0;
  long long res = 0;
  for (i = 1; i <= top; ++i) {
    ra = find(E[i].from);
    rb = find(E[i].to);
    if (ra != rb) {
      root[ra] = rb;
      res += E[i].len;
      if (++intree == n - 1) return res;
    }
  }
  return -1;
}
int main() {
  cin >> n;
  int i;
  for (i = 1; i <= n; ++i) cin >> x[i] >> y[i];
  S[0].x = S[0].y = 0x3f3f3f3f;
  for (i = 1; i <= n; ++i) S[i] = Node(x[i], y[i], i);
  AddEdge();
  for (i = 1; i <= n; ++i) S[i] = Node(y[i], x[i], i);
  AddEdge();
  for (i = 1; i <= n; ++i) S[i] = Node(-y[i], x[i], i);
  AddEdge();
  for (i = 1; i <= n; ++i) S[i] = Node(x[i], -y[i], i);
  AddEdge();
  printf("%lld\n", Kruskal());
  return 0;
}
