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: Wrong Answer)

#include <iostream>
#include <iomanip>
#include <array>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <numeric>
#include <cassert>
#include <functional>
using namespace std;

typedef unsigned long long int llui;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
constexpr double pi = 3.141592653589793238462643383279502884L;

// TODO: Mod class

const int sz = 1e5 + 5;
int group[sz];
typedef pair<int, pii> piii;

int Find(int n) {
  int g = group[n];
  if (g != n) {
    group[n] = Find(g);
  }
  return group[n];
}

void Union(int n, int m) {
  int g1 = Find(n);
  int g2 = Find(m);
  if (g1 != g2) {
    group[g1] = g2;
  }
}

piii make_piii(int a, int b, int c) {
  return make_pair(a, make_pair(b, c));
}

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<pii> ori(n);
  for (int i = 0; i < n; ++i) {
    cin >> ori[i].first >> ori[i].second;
  }
  sort(ori.begin(), ori.end());
  ori.resize(unique(ori.begin(), ori.end()) - ori.begin());

  vector<pii> x;
  vector<pii> y;
  for (int i = 0; i < ori.size(); ++i) {
    int a, b;
    a = ori[i].first;
    b = ori[i].second;
    x.push_back(make_pair(a, i));
    y.push_back(make_pair(b, i));
  }

  sort(x.begin(), x.end());
  sort(y.begin(), y.end());

  vector<piii> xdiff;
  vector<piii> ydiff;

  for (int i = 0; i < xdiff.size(); ++i) {
    xdiff.push_back(make_piii(abs(x[i].first - x[i + 1].first), x[i].second, x[i + 1].second));
    ydiff.push_back(make_piii(abs(y[i].first - y[i + 1].first), y[i].second, y[i + 1].second));
  }

  sort(xdiff.begin(), xdiff.end());
  sort(ydiff.begin(), ydiff.end());

  for (int i = 0; i < n; ++i) {
    group[i] = i;
  }

  int fx = 0;
  int fy = 0;
  ll cost = 0;
  for (int i = 0; i < ori.size() - 1; ++i) {
    piii now;
    if (fx == xdiff.size() && fy == ydiff.size()) {
      break;
    }
    if (fx == xdiff.size()) {
      now = ydiff[fy++];
    } else if (fy == ydiff.size()) {
      now = xdiff[fx++];
    } else {
      if (xdiff[fx].first < ydiff[fy].first) {
        now = xdiff[fx++];
      } else {
        now = ydiff[fy++];
      }
    }
    
    if (Find(now.second.first) == Find(now.second.second)) {
      --i;
      continue;
    }
    Union(now.second.first, now.second.second);
    cost += now.first;
  }

  cout << cost << endl;
}