n = gets.to_i
array = Array.new(n){gets.split(" ").map(&:to_i)}

dp = [array[0].max, array[0].index(array[0].max)]

sum = dp[0]
(n-1).times do |i|
  if dp[1] == array[i+1].index(array[i+1].max)
    max = array[i+1].sort[1]
  else
    max = array[i+1].max
  end
  dp_next = [max, array[i+1].index(max)]
  dp = dp_next
  sum += dp[0]
end

puts sum