d = []
n = gets.to_i
t = Array.new()
n.times do |i|
  a, b = gets.chomp.split.map(&:to_i)
  t.push([a, 0, i])
  t.push([b, 1, i])
end
t.sort!

used = Array.new(1e5, false)
c = [0, 0]

a = 0
for i in 0...n
  a += t[i][0]
  c[t[i][1]] += 1
  used[t[i][2]] = true
end

if c[0] > 0 && c[1] > 0 && used.select{|i| i}.length == n
  if t[n-1][2] == t[n][2] && c[t[n-1][1]] > 1
    a += [t[n+1][0] - t[n-1][0], t[n][0] - t[n-2][0]].min
  else
    a += t[n][0] - t[n-1][0]
  end
end
puts a