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: def solve_abc065d():
      from collections import namedtuple
      from heapq import heappush, heappop
      from operator import attrgetter
      from itertools import tee
      import sys
  
      input = sys.stdin.readline
  
      City = namedtuple("City", "x y idx")
      Edge = namedtuple("Edge", "cost to")
      N = int(input())
  
      cities = []
      for city_idx in range(N):
          x, y = map(int, input().split())
          c_ = City(x, y, city_idx)
          cities.append(c_)
      cities_x_asc = sorted(cities, key=attrgetter("x"))
      cities_y_asc = sorted(cities, key=attrgetter("y"))
  
      graph = tuple(set() for _ in range(N))
  
      it_x = iter(cities_x_asc)
      gx1, gx2 = tee(it_x, 2)
      next(gx2)
      for c1, c2 in zip(gx1, gx2):
          d_ = c2.x - c1.x
          graph[c1.idx].add(Edge(d_, c2.idx))
          graph[c2.idx].add(Edge(d_, c1.idx))
      # x方向にN-1本の辺を張る
  
      it_y = iter(cities_y_asc)
      gy1, gy2 = tee(it_y, 2)
      next(gy2)
      for c1, c2 in zip(gy1, gy2):
          d_ = c2.y - c1.y
          graph[c1.idx].add(Edge(d_, c2.idx))
          graph[c2.idx].add(Edge(d_, c1.idx))
      # y方向にN-1本の辺を張る
  
      # <Prim’s Algorithm>
      # https://tjkendev.github.io/procon-library/python/graph/min_st_prim.html
      # プリム法では、グラフGの1頂点からなる木から始め、
      # 木に含まれる頂点と木に含まれない頂点を繋ぐ辺のうち、
      # 一番コストが小さい辺が繋ぐ(木に含まれない方の)頂点を木に追加する、
      # ことを繰り返すことで最小全域木を求める。
      # G[v] = [w, ...]
      #     グラフG上で頂点vが隣接する辺集合
  
      start = 0  # 起点とする頂点
  
      used = [-1] * N
      used[start] = 1
  
      h = []
      for e_ in graph[start]:
          heappush(h, e_)
  
      mi = 0
      while h:
          e_ = heappop(h)
          if ~used[e_.to]:
              continue
  
          used[e_.to] = 1
          mi += e_.cost
  
          for ne_ in graph[e_.to]:
              if ~used[ne_.to]:
                  continue
              heappush(h, ne_)
  
      # miが最小全域木の解
  
      print(mi)
      return
  
  
  if __name__ == "__main__":
      solve_abc065d()
  
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"}]