main :: IO ()
main = print . solve . map read . words =<< getContents

solve :: [Integer] -> Integer
solve (ai:ao:at:aj:al:_)
  | aj > 1 = 2 * (aj `div` 2) + solve [ai, ao, at, aj `mod` 2, al]
  | al > 1 = 2 * (al `div` 2) + solve [ai, ao, at, aj, al `mod` 2]
  | aj == 1 && al == 1 && ai > 0 = 3 + solve [ai - 1, ao, at, aj - 1, al - 1]
  | ai > 1 = 2 * (ai `div` 2) + solve [ai `mod` 2, ao, at, aj, al]
  | otherwise = ao
