DBG = !true

n = gets.to_i
a = Array.new(n)
b = Array.new(n)
st = []
for i in 0...n
  a1, b1 = gets.split.map{|x| x.to_i}
  a[i] = a1
  b[i] = b1
  st << [a1, i, 0]
  st << [b1, i, 1]
end

st.sort_by!{|x| x[0]}

cnts = Array.new(n, 0)
bothFound = false
aFound = false
bFound = false
halfsum = 0
for i in 0...n
  halfsum += st[i][0]
  suf = st[i][1]
  cnts[suf] += 1
  bothFound = true if cnts[suf] > 1
  aFound = true if st[i][2]>0
  bFound = true if st[i][2]==0
end

print halfsum, "\n" if DBG

if bothFound || !aFound || !bFound
  print "found case\n" if DBG
  print halfsum
  exit
end

h0last  = st[n-1]
h1first = st[n]

if h0last[1] != h1first[1]
  print "no found case 1\n" if DBG
  print (halfsum + h1first[0] - h0last[0])
  exit
end

h0last2  = st[n-2]
h1first2 = st[n+1]

d = [(h1first[0] - h0last2[0]).abs, (h1first2[0] - h0last[0]).abs].min
print (halfsum + d)
