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)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*----------------------------------------*/
struct UnionFind {
  UnionFind(int sz) : data(sz, -1) {}

  int root(int ver) { return data[ver] < 0 ? ver : data[ver] = root(data[ver]); }

  void unite(int ver1, int ver2) {
    ver1 = root(ver1);
    ver2 = root(ver2);
    if (ver1 != ver2) {
      if (data[ver1] > data[ver2]) swap(ver1, ver2);
      data[ver1] += data[ver2];
      data[ver2] = ver1;
    }
  }

  bool same(int ver1, int ver2) { return root(ver1) == root(ver2); }

  int size(int ver) { return -data[root(ver)]; }

private:
  vector<int> data;
};

using CostType = long long;
struct Edge {
  int src, dst;
  CostType cost;
  Edge(int dst_, CostType cost_ = 0) : src(-1), dst(dst_), cost(cost_) {}
  Edge(int src_, int dst_, CostType cost_) : src(src_), dst(dst_), cost(cost_) {}
};
inline bool operator < (const Edge &lhs, const Edge &rhs) { return lhs.cost < rhs.cost; }
inline bool operator <= (const Edge &lhs, const Edge &rhs) { return lhs.cost <= rhs.cost; }
inline bool operator > (const Edge &lhs, const Edge &rhs) { return lhs.cost > rhs.cost; }
inline bool operator >= (const Edge &lhs, const Edge &rhs) { return lhs.cost >= rhs.cost; }

CostType kruskal(const vector<vector<Edge> > &graph) {
  int sz = graph.size();
  priority_queue<Edge, vector<Edge>, greater<Edge> > que;
  REP(i, sz) for (Edge e : graph[i]) que.emplace(e);
  vector<vector<Edge> > res(sz);
  CostType total = 0;
  UnionFind uf(sz);
  while (!que.empty()) {
    Edge e = que.top(); que.pop();
    if (!uf.same(e.src, e.dst)) {
      res[e.src].emplace_back(e);
      res[e.dst].emplace_back(Edge(e.dst, e.src, e.cost));
      total += e.cost;
      uf.unite(e.src, e.dst);
    }
  }
  // return res;
  return total;
}

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n; cin >> n;
  vector<pair<pair<int, int>, int> > x(n), y(n);
  REP(i, n) {
    int xx, yy; cin >> xx >> yy;
    x[i] = {{xx, yy}, i};
    y[i] = {{yy, xx}, i};
  }
  sort(ALL(x));
  sort(ALL(y));
  vector<vector<Edge> > graph(n);
  REP(i, n-1) {
    long long cost = min(abs(x[i].first.first-x[i+1].first.first), abs(x[i].first.second-x[i+1].first.second));
    int num1 = x[i].second, num2 = x[i+1].second;
    graph[num1].emplace_back(Edge(num1, num2, cost));
  }
  REP(i, n-1) {
    long long cost = min(abs(y[i].first.first-y[i+1].first.first), abs(y[i].first.second-y[i+1].first.second));
    int num1 = y[i].second, num2 = y[i+1].second;
    graph[num1].emplace_back(Edge(num1, num2, cost));
  }
  cout << kruskal(graph) << '\n';
  return 0;
}