output_description: 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.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      x_1 y_1
      x_2 y_2
      :
      x_N y_N
attempt: #include<iostream>
  #include<iomanip>
  #include<cstdio>
  
  #include<vector>
  #include<queue>
  #include<set>
  #include<map>
  #include<string>
  
  #include<algorithm>
  #include<cmath>
  #include<numeric>
  
  using namespace std;
  typedef long long ll;
  template <class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}
  template <class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
  
  
  /*
  木にすべき
  N-1本
  
  最小全域木を作りたい
  
  可能性のある道は、x,yそれぞれで隣接した辺のみで合計候補2nぐらい
  これにクラスカル?
  */
  
  
  const int  MAX_N = 100000;
  int par[MAX_N];
  int rk[MAX_N];
  
  //配列が長さnのものとして0で初期化する
  void init(int n) {
      for (int i = 0; i < n; i++) {
          par[i] = i;
          rk[i] = 0;
      }
  }
  
  //xの親の要素の番号を返す
  int find(int x) {
      if (par[x] == x) {
          return x;
      } else {
          return par[x] = find(par[x]);
      }
  }
  
  //要素を併合する
  //木の高さrkが低いほうを高いほうの親につなげる
  void unite(int x, int y){
      x = find(x);
      y = find(y);
      if (x == y) return;
  
      if (rk[x] < rk[y]) {
          par[x] = y;
      } else {
          par[y] = x;
          if (rk[x] == rk[y]) rk[x]++;
      }
  }
  
  //要素が同じ集合に属しているかどうかを返す
  bool same(int x, int y) {
      return find(x) == find(y);
  }
  
  
  
  const int MAX_E = 2e5;
  struct edge {int u, v, cost;};
  bool comp(const edge& e1, const edge& e2) {
      return e1.cost < e2.cost;
  }
  edge es[MAX_E];
  int V, E;
  
  //クラスカル法 UNION_FINDを使って既に同じ木にあるかどうか判定する
  ll kruskal() {
      sort(es, es + E, comp);
      init(V);
      int res = 0;
      for (int i = 0; i < E; i++) {
          edge e = es[i];
          if (!same(e.u, e.v)) {
              unite(e.u,e.v);
              res += (ll)e.cost;
          }
      }
      return res;
  }
  
  int main() {
  
      int N; cin >> N;
      vector<pair<ll,ll> > x(N), y(N);
      for (int i = 0; i < N; i++) {
          ll s,t; cin >> s >> t;
          x[i] = make_pair(s,i);
          y[i] = make_pair(t,i);
      }
      //argsortしないとだめ
      sort(x.begin(),x.end());
      sort(y.begin(),y.end());
      for (int i = 0; i < N-1; i++) {
          edge e1, e2;
          e1.u = x[i].second; e2.u = y[i].second;
          e1.v = x[i+1].second; e2.v = y[i+1].second;
          e1.cost = abs(x[i].first-x[i+1].first);
          e2.cost = abs(y[i].first-y[i+1].first);
          es[i] = e1; es[i+N-1] = e2;
      }
  
      V = N; E = 2 * N - 2;
  
      ll res = kruskal();
      cout << res << endl;
  
  
  }
  
problem_description: 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?
samples: [{"input": "3\n    1 5\n    3 9\n    7 8", "output": "3\n    \n\nBuild a road between Towns 1 and 2, and another between Towns 2 and 3. The\ntotal cost is 2+1=3 yen.\n\n* * *"}, {"input": "6\n    8 3\n    4 9\n    12 19\n    18 1\n    13 5\n    7 6", "output": "8"}]