Problem:
Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

// https://atcoder.jp/contests/abc065/tasks/arc076_b

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n)   for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for(int i=(b); i<=(int)(e); i++)
#define DUMP(a, n) REP(_i, n) printf("%d%c", a[_i], _i + 1 == n ? '\n' : ' ')
#define DUMP2D(a, n, m) REP(_i, n) REP(_j, m) printf("%d%c", a[_i][_j], _j + 1 == m ? '\n' : ' '); puts("")

//------------------------------------------------------------------------------
class UnionFindTree {
public:
  int n;
  vector<int> par;
  vector<int> rnk;

  void init(int _n) {
    n = _n;
    par.resize(n);
    rnk.resize(n, 0);
    REP(i, n) par[i] = i;
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;

    if (rnk[x] < rnk[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rnk[x] == rnk[y]) rnk[x]++;
    }
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }
};

//------------------------------------------------------------------------------

const int N_MAX = 1e5;
const int X_I_MAX = 1e9;
const int Y_I_MAX = 1e9;

struct city { int id, x, y; };
struct dist { int i, j, d; };

int N;
// int x[N_MAX];
// int y[N_MAX];

city cs[N_MAX];
dist ds[N_MAX * 2];

UnionFindTree uf;

bool compare_x_asc(const city& c1, const city& c2) {
  return c1.x < c2.x;
}

bool compare_y_asc(const city& c1, const city& c2) {
  return c1.y < c2.y;
}

bool compare_d_asc(const dist& d1, const dist& d2) {
  return d1.d < d2.d;
}

void solve() {
  uf.init(N);
  sort(cs, cs + N, compare_x_asc);
  REP(i, N - 1) ds[i] = { cs[i].id, cs[i + 1].id, cs[i + 1].x - cs[i].x };
  sort(cs, cs + N, compare_y_asc);
  REP(i, N - 1) ds[N - 1 + i] = { cs[i].id, cs[i + 1].id, cs[i + 1].y - cs[i].y };
  int M = (N - 1) * 2;
  sort(ds, ds + M, compare_d_asc);

  int ans = 0;
  REP(k, M) {
    dist d = ds[k];
    if (uf.same(d.i, d.j)) continue;
    uf.unite(d.i, d.j);
    ans += d.d;
  }
  printf("%d\n", ans);
}

void input() {
  scanf("%d", &N);
  REP(i, N) {
    int x, y;
    scanf("%d%d", &x, &y);
    cs[i] = {i, x, y};
  }
}

int main() {
  input();
  solve();
  return 0;
}