module Main where type Input = [Int] type Output = Int parse :: String -> Input parse = map read . words . head . lines solve :: Input -> Output solve input = let [a, b, c, d, e] = input b1 = repeat True b2 = repeat False a' = take a b1 b' = take b b2 c' = take c b1 d' = take d b2 e' = lcm (a + b) (c + d) (n, m) = e `divMod` e' xs = cycle $ a' ++ b' ys = cycle $ c' ++ d' f = length . filter id . zipWith (&&) xs . flip take ys in n * f e' + f m render :: Output -> String render = show main :: IO () main = interact ( render . solve . parse )