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: #!/usr/bin/env python3
  from collections import defaultdict
  from collections import deque
  from heapq import heappush, heappop
  import sys
  import math
  import bisect
  import random
  import itertools
  sys.setrecursionlimit(10**5)
  stdin = sys.stdin
  bisect_left = bisect.bisect_left
  bisect_right = bisect.bisect_right
  def LI(): return list(map(int, stdin.readline().split()))
  def LF(): return list(map(float, stdin.readline().split()))
  def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
  def II(): return int(stdin.readline())
  def IF(): return float(stdin.readline())
  def LS(): return list(map(list, stdin.readline().split()))
  def S(): return list(stdin.readline().rstrip())
  def IR(n): return [II() for _ in range(n)]
  def LIR(n): return [LI() for _ in range(n)]
  def FR(n): return [IF() for _ in range(n)]
  def LFR(n): return [LI() for _ in range(n)]
  def LIR_(n): return [LI_() for _ in range(n)]
  def SR(n): return [S() for _ in range(n)]
  def LSR(n): return [LS() for _ in range(n)]
  mod = 1000000007
  inf = float('INF')
  
  #A
  def A():
      x, a, b = LI()
      if b <= a:
          print("delicious")
      elif b <= a + x:
          print("safe")
      else:
          print("dangerous")
  
      return
  
  #B
  def B():
      n = II()
      a = IR(n)
      check = [False] * (n + 1)
      check[1] = True
      now = a[0]
      for i in range(1,n+1):
          if check[now]:
              break
          if now == 2:
              print(i)
              return
          check[now] = True
          now = a[now - 1]
      print(-1)
      return
  
  #C
  def C():
      return
  
  #D
  def D():
      def find(x):
          if x == par[x]:
              return x
          par[x] = find(par[x])
          return par[x]
      def same(x, y):
          return find(x) == find(y)
      def unite(x, y):
          x = find(x)
          y = find(y)
          par[y] = x
          return True
      def solve(a, k):
          for i in range(n-1):
              q0, q1 = xy[i][k], xy[i + 1][k]
              z, v = xy[i][2], xy[i + 1][2]
              edg[a + i] = [q1 - q0, z, v]
      n = II()
      par = [i for i in range(n)]
      xy = [None] * n
      for i in range(n):
          x, y = LI()
          xy[i] = [x, y, i]
      xy.sort(key=lambda x: x[0])
      edg = [None] * (2 * n - 2)
      solve(0, 0)
      xy.sort(key=lambda x: x[1])
      solve(n - 1, 1)
      ans = 0
      a = 0
      edg.sort(key=lambda x: x[0])
      for i in range(2 * n - 2):
          value, p0, p1 = edg[i]
          if a == n - 1:
              break
          if not same(p0, p1):
              unite(p0, p1):
              a += 1
              ans += value
      print(ans)
      return
  
  #Solve
  if __name__ == '__main__':
      D()
  
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"}]