#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
const int maxm = 1000 * 1000 + 10;
struct node {
  int x;
  int y;
  int id;
} a[100009], b[100009];
const bool com1(const node &a, const node &b) { return a.x < b.x; }
const bool com2(const node &a, const node &b) { return a.y < b.y; }
struct Edge {
  int u, v;
  long long dist;
  Edge() {}
  Edge(int u, int v, long long d) : u(u), v(v), dist(d) {}
  bool operator<(const Edge &rhs) const { return dist < rhs.dist; }
};
struct Kruskal {
  int n, m;
  Edge edges[maxm];
  int fa[maxn];
  int findset(int x) { return fa[x] == -1 ? x : fa[x] = findset(fa[x]); }
  void init(int n) {
    this->n = n;
    m = 0;
    memset(fa, -1, sizeof(fa));
  }
  void AddEdge(int u, int v, long long dist) { edges[m++] = Edge(u, v, dist); }
  long long kruskal() {
    long long sum = 0;
    int cnt = 0;
    sort(edges, edges + m);
    for (int i = 0; i < m; i++) {
      int u = edges[i].u, v = edges[i].v;
      if (findset(u) != findset(v)) {
        sum += edges[i].dist;
        fa[findset(u)] = findset(v);
        if (++cnt >= n - 1) break;
      }
    }
    if (cnt < n - 1) return -1;
    return sum;
  }
} KK;
int main() {
  int n;
  cin >> n;
  KK.init(n);
  for (int i = 1; i <= n; i++) {
    int u, v;
    cin >> u >> v;
    a[i].x = u;
    a[i].y = v;
    a[i].id = i;
    b[i].x = u;
    b[i].y = v;
    b[i].id = i;
  }
  sort(a + 1, a + 1 + n, com1);
  sort(b + 1, b + 1 + n, com2);
  for (int i = 1; i <= n - 1; i++) {
    KK.AddEdge(a[i].id, a[i + 1].id, abs(a[i].x - a[i + 1].x));
    KK.AddEdge(b[i].id, b[i + 1].id, abs(b[i].y - b[i + 1].y));
  }
  long long ans = KK.kruskal();
  cout << ans << endl;
}
