import sys

N=int(eval(input()))

A=0

B=0

table=[]

for i in range(N):

    a,b=list(map(int,input().split()))

    table.append((a,i))

    table.append((b,i))

    A+=a

    B+=b

ans=min(A,B)

table.sort()

num=0

for i in range(N):

    num+=table[i][0]

L=[0]*N

#print(table)

for i in range(N):

    c,j=table[i]

    if L[j]!=0:

        ans=min(num,ans)

        print(ans)

        #print(L)

        sys.exit()

    L[j]+=1

c,n=table[N-1]

if n!=table[N][1]:

    num=num-table[N-1][0]+table[N][0]

    ans = min(num, ans)

    print(ans)

    sys.exit()

num1=num-table[N-1][0]+table[N+1][0]

num2=num-table[N-2][0]+table[N][0]

print((min(ans,num1,num2)))





import sys

input = sys.stdin.readline

N=int(eval(input()))

A=0

B=0

table=[]

for i in range(N):

    a,b=list(map(int,input().split()))

    table.append((a,i))

    table.append((b,i))

    A+=a

    B+=b

ans=min(A,B)

table.sort()

num=0

for i in range(N):

    num+=table[i][0]

L=[0]*N

#print(table)

for i in range(N):

    c,j=table[i]

    if L[j]!=0:

        ans=min(num,ans)

        print(ans)

        #print(L)

        sys.exit()

    L[j]+=1

c,n=table[N-1]

if n!=table[N][1]:

    num=num-table[N-1][0]+table[N][0]

    ans = min(num, ans)

    print(ans)

    sys.exit()

num1=num-table[N-1][0]+table[N+1][0]

num2=num-table[N-2][0]+table[N][0]

print((min(ans,num1,num2)))





import sys

N = int(eval(input()))
A = 0
B = 0
table = []
for i in range(N):
    a, b = list(map(int, input().split()))
    table.append((a, i))
    table.append((b, i))
    A += a
    B += b
ans = min(A, B)
table.sort()
num = 0
for i in range(N):
    num += table[i][0]
L = [0] * N
# print(table)
for i in range(N):
    c, j = table[i]
    if L[j] != 0:
        ans = min(num, ans)
        print(ans)
        # print(L)
        sys.exit()
    L[j] += 1
c, n = table[N - 1]
if n != table[N][1]:
    num = num - table[N - 1][0] + table[N][0]
    ans = min(num, ans)
    print(ans)
    sys.exit()
num1 = num - table[N - 1][0] + table[N + 1][0]
num2 = num - table[N - 2][0] + table[N][0]
print((min(ans, num1, num2)))

import sys

input = sys.stdin.readline
N = int(eval(input()))
A = 0
B = 0
table = []
for i in range(N):
    a, b = list(map(int, input().split()))
    table.append((a, i))
    table.append((b, i))
    A += a
    B += b
ans = min(A, B)
table.sort()
num = 0
for i in range(N):
    num += table[i][0]
L = [0] * N
# print(table)
for i in range(N):
    c, j = table[i]
    if L[j] != 0:
        ans = min(num, ans)
        print(ans)
        # print(L)
        sys.exit()
    L[j] += 1
c, n = table[N - 1]
if n != table[N][1]:
    num = num - table[N - 1][0] + table[N][0]
    ans = min(num, ans)
    print(ans)
    sys.exit()
num1 = num - table[N - 1][0] + table[N + 1][0]
num2 = num - table[N - 2][0] + table[N][0]
print((min(ans, num1, num2)))

+input = sys.stdin.readline