# C - Vacation
N = gets.to_i

l2,l1 = gets.split.map.with_index {|n,i| [n.to_i,i] }.sort[1,2]
readlines.each do |s|
  a2,a1 = s.split.map.with_index {|n,i| [n.to_i,i] }.sort[1,2]
  if a1[1] == l1[1]
    n1 = l2[0]+a1[0]
    n2 = l1[0]+a2[0]
    if n1 < n2
      l1 = [n2, a2[1]]
      l2 = [n1, a1[1]]
    else
      l1 = [n1, a1[1]]
      l2 = [n2, a2[1]]
    end
  else
    l2 = [l1[0]+a2[0], a2[1]]
    l1 = [l1[0]+a1[0], a1[1]]
  end
end
puts l1[0]
