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: import heapq
  
  N = int(input())
  iXY = [[i] + list(map(int, input().split())) for i in range(N)]
  
  G = [set() for _ in range(N)]
  for k in [1, 2]:
      iXY.sort(key=lambda a: a[k])
      for i in range(1, N):
          i0 = iXY[i - 1][0]
          i1 = iXY[i][0]
          G[i0].add((i1))
          G[i1].add((i0))
  
  Q = [(0, 0)]
  B = set()
  a = 0
  iXY.sort()
  
  while True:
      d, v = heapq.heappop(Q)
      while v in B:
          d, v = heapq.heappop(Q)
      a += d
      B.add(v)
      if len(B) == N:
          break
      for i in G[v]:
          if i not in B:
              heapq.heappush(
                  Q, (min(abs(iXY[v][1] - iXY[i][1]), abs(iXY[v][2] - iXY[i][2])), i)
              )
  print(a)
  
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"}]