You are given a directed weighted graph with N vertices, each having two integers A_i and B_i. The weight of an edge from vertex x to vertex y is the minimum of A_x and B_y. Find the minimum total weight of the edges in a directed cycle that visits every vertex exactly once. Constraints: 2 ≤ N ≤ 10^5, 1 ≤ A_i, B_i ≤ 10^9. Input is given as N followed by N lines of A_i and B_i. Output the minimum total weight of the edges in such a cycle.

# Buggy version:
N=int(input());A=[-1]*N;B=[-1]*N
for i in range(N):A[i],B[i]=map(int,input().split())
ans=min(sum(A),sum(B));t=[(A[i],i) for i in range(N)]+[(B[i],i) for i in range(N)];t.sort();d=[0]*N
for i in range(N):d[t[i][1]]+=1
S=sum(t[i][0] for i in range(N))
if max(d)==2:
    print(S)
else:
    ans=min(S+max(A[i],B[i])-t[N-1-(t[N-1][1]==i)][0] for i in range(N))
    print(ans)


# Fixed version:
N=int(input());A=[-1]*N;B=[-1]*N
for i in range(N):A[i],B[i]=map(int,input().split())
ans=min(sum(A),sum(B));t=[(A[i],i) for i in range(N)]+[(B[i],i) for i in range(N)];t.sort();d=[0]*N
for i in range(N):d[t[i][1]]+=1
S=sum(t[i][0] for i in range(N))
if max(d)==2:
    print(S)
else:
    ans2=min(S+max(A[i],B[i])-t[N-1-(t[N-1][1]==i)][0] for i in range(N))
    print(min(ans,ans2))

# Buggy version:
N=int(input())
A=[]
node=[]
for i in range(N):
  x,y=map(int,input().split())
  A.append((x,i,"left"))
  A.append((y,i,"right"))
  node.append((x,y))
  
A.sort()
Used=[[False,False] for i in range(N)]
Used_node=0
for i in range(N):
  if A[i][2]=="left":
    Used[i][0]=True
    if not Used[i][1]:
      Used_node+=1
  if A[i][2]=="right":
    Used[i][1]=True
    if not Used[i][0]:
      Used_node+=1
if Used_node!=N:
  ans=0
  for i in range(N):
    ans+=A[i][0]
  print(ans)
else:
  if A[0][0]:
    f=0
  else:
    f=1
  NO=0
  for i in range(N):
    if not Used[i][f]:
      NO=1
      break
  ans=0
  for i in range(N):
    ans+=A[i][0]
  ans2=float("inf")
  if NO==1:
    for w in range(N):
      if A[N][1]==w:
        ans2=min(ans2,ans-min(node[w])+A[N+1][0])
      else:
        ans2=min(ans2,ans-min(node[w])+A[N][0])
  print(ans)
    

# Fixed version:
N=int(input())
A=[]
node=[]
for i in range(N):
  x,y=map(int,input().split())
  A.append((x,i,"left"))
  A.append((y,i,"right"))
  node.append((x,y))
  
A.sort()
Used=[[False,False] for i in range(N)]
Used_node=0
for i in range(N):
  if A[i][2]=="left":
    Used[A[i][1]][0]=True
    if not Used[A[i][1]][1]:
      Used_node+=1
  if A[i][2]=="right":
    Used[A[i][1]][1]=True
    if not Used[A[i][1]][0]:
      Used_node+=1
if Used_node!=N:
  ans=0
  for i in range(N):
    ans+=A[i][0]
  print(ans)
else:
  if Used[0][0]:
    f=0
  else:
    f=1
  NO=0
  for i in range(N):
    if not Used[i][f]:
      NO=1
      break
  ans=0
  for i in range(N):
    ans+=A[i][0]
  ans2=float("inf")
  if NO==1:
    for w in range(N):
      if A[N][1]==w:
        ans2=min(ans2,ans-min(node[w])+A[N+1][0])
      else:
        ans2=min(ans2,ans-min(node[w])+A[N][0])
    print(ans2)
    exit()
  print(ans)

# Buggy version:
n=int(input())
a=[]
b=[]
for i in range(n):
    p,q=map(int,input().split())
    a.append((p,i,0))
    a.append((q,i,1))
a.sort(key=lambda x:x[0])
result=0
l=[0]*n
flag=0
for i in range(n):
    if l[a[i][1]]==1:
        flag=1
        break
    else:
        l[a[i][1]]=1
if flag==1:

    for i in range(n):
        result+=a[i][0]
    print(result)
else:
    if a[n-1][1]==a[n][1]:
        for i in range(n):
            result+=a[i][0]
        result+=min(a[n+1][0]-a[n-1][0],a[n][0]-a[n-2][0])
        print(result)
    else:
        for i in range(n):
            result+=a[i][0]
        result+=a[n][0]-a[n-1][0]
        print(result)
        

# Fixed version:
n=int(input())
a=[]
b=[]
for i in range(n):
    p,q=map(int,input().split())
    a.append((p,i,0))
    a.append((q,i,1))
a.sort(key=lambda x:x[0])
result=0
l=[0]*n
flag=0
for i in range(n):
    if l[a[i][1]]==1:
        flag=1
        break
    else:
        l[a[i][1]]=1
t=a[0][2]
s=0
for i in range(n):
    s+=a[i][2]-a[0][2]
if s==0:
    flag=1
if flag==1:

    for i in range(n):
        result+=a[i][0]
    print(result)
else:
    if a[n-1][1]==a[n][1]:
        for i in range(n):
            result+=a[i][0]
        result+=min(a[n+1][0]-a[n-1][0],a[n][0]-a[n-2][0])
        print(result)
    else:
        for i in range(n):
            result+=a[i][0]
        result+=a[n][0]-a[n-1][0]
        print(result)