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: n = int(input())
  li = []
  for i in range(n):
      li.append(list(map(int, input().split())))
  
  
  li = sorted(li, key=lambda x: (x[1], -x[0]))
  li.reverse()
  
  li2 = sorted(li, key=lambda x: (x[0], -x[1]))
  li2.reverse()
  
  
  li3 = sorted(li, key=lambda x: (x[1], x[0]))
  li3.reverse()
  
  li4 = sorted(li, key=lambda x: (x[0], x[1]))
  li4.reverse()
  
  li5 = sorted(li, key=lambda x: (-x[1], -x[0]))
  li5.reverse()
  
  li6 = sorted(li, key=lambda x: (-x[0], -x[1]))
  li6.reverse()
  
  
  li7 = sorted(li, key=lambda x: (-x[1], x[0]))
  li7.reverse()
  
  li8 = sorted(li, key=lambda x: (-x[0], x[1]))
  li8.reverse()
  
  
  length = 0
  length2 = 0
  length3 = 0
  length4 = 0
  length5 = 0
  length6 = 0
  length7 = 0
  length8 = 0
  
  for i in range(len(li) - 1):
      length += min(li[i][0], li[i + 1][1])
  
  length += min(li[-1][0], li[0][1])
  
  for i in range(len(li) - 1):
      length2 += min(li2[i][0], li2[i + 1][1])
  
  length2 += min(li2[-1][0], li2[0][1])
  
  for i in range(len(li) - 1):
      length3 += min(li3[i][0], li3[i + 1][1])
  
  length3 += min(li3[-1][0], li3[0][1])
  for i in range(len(li) - 1):
      length4 += min(li4[i][0], li4[i + 1][1])
  
  length4 += min(li4[-1][0], li4[0][1])
  
  for i in range(len(li) - 1):
      length5 += min(li5[i][0], li5[i + 1][1])
  
  length5 += min(li5[-1][0], li5[0][1])
  
  for i in range(len(li) - 1):
      length6 += min(li6[i][0], li6[i + 1][1])
  
  length6 += min(li6[-1][0], li6[0][1])
  for i in range(len(li) - 1):
      length7 += min(li7[i][0], li7[i + 1][1])
  
  length7 += min(li7[-1][0], li7[0][1])
  for i in range(len(li) - 1):
      length8 += min(li8[i][0], li8[i + 1][1])
  
  length8 += min(li8[-1][0], li8[0][1])
  
  hikaku = [length, length2, length3, length4, length5, length6, length7, length8]
  
  print(min(hikaku))
  
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"}]