output_description: Print the minimum total weight of the edges in such a cycle.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      A_1 B_1
      A_2 B_2
      :
      A_N B_N
attempt: mod = 1000000007
  eps = 10**-9
  
  
  def main():
      import sys
      from heapq import heappush, heappop, heapify
  
      input = sys.stdin.buffer.readline
  
      N = int(input())
      L = []
      R = []
      LR = []
      for i in range(N):
          a, b = map(int, input().split())
          L.append((-a, i))
          R.append((b, i))
          LR.append((a, b))
      heapify(L)
      heapify(R)
  
      used = set()
      ans = 0
      for new_id in range(N, N * 2 - 1):
          while True:
              l_, id_L = heappop(L)
              l = -l_
              if id_L in used:
                  continue
              else:
                  break
          while True:
              r, id_R = heappop(R)
              if id_R in used:
                  continue
              else:
                  break
          if id_L == id_R:
              while True:
                  l_2, id_L2 = heappop(L)
                  l2 = -l_2
                  if id_L2 in used:
                      continue
                  else:
                      heappush(L, (l_, id_L))
                      l = l2
                      id_L = id_L2
                      break
          used.add(id_L)
          used.add(id_R)
          ans += min(LR[id_L][0], LR[id_R][1])
          LR.append((LR[id_R][0], LR[id_L][1]))
          heappush(L, (-LR[id_R][0], new_id))
          heappush(R, (LR[id_L][1], new_id))
  
      ans += min(LR[-1][0], LR[-1][1])
      print(ans)
  
  
  if __name__ == "__main__":
      main()
  
problem_description: Statement
  
  We have a directed weighted graph with N vertices. Each vertex has two
  integers written on it, and the integers written on Vertex i are A_i and B_i.
  
  In this graph, there is an edge from Vertex x to Vertex y for all pairs 1 \leq
  x,y \leq N, and its weight is {\rm min}(A_x,B_y).
  
  We will consider a directed cycle in this graph that visits every vertex
  exactly once. Find the minimum total weight of the edges in such a cycle.
samples: [{"input": "3\n    1 5\n    4 2\n    6 3", "output": "7\n    \n\nConsider the cycle 1\u21923\u21922\u21921. The weights of those edges are {\\rm\nmin}(A_1,B_3)=1, {\\rm min}(A_3,B_2)=2 and {\\rm min}(A_2,B_1)=4, for a total of\n7. As there is no cycle with a total weight of less than 7, the answer is 7.\n\n* * *"}, {"input": "4\n    1 5\n    2 6\n    3 7\n    4 8", "output": "10\n    \n\n* * *"}, {"input": "6\n    19 92\n    64 64\n    78 48\n    57 33\n    73 6\n    95 73", "output": "227"}]