import Control.Monad

main = do
    xs <- map read <$> words <$> getLine :: IO [Integer]
    if all (== 0) xs then return ()
    else do
        let [a1,_,a2,_,a3,_] = xs
            [_,m1,_,m2,_,m3] = xs
            as = [a1,a2,a3]
            ms = [m1,m2,m3]
            cycles = zipWith solve as ms
        print $ foldr1 lcm cycles
        main

solve :: Integer -> Integer -> Integer
solve a m = rec 1 $ a `mod` m
    where rec c 1 = c
          rec c x = rec (c+1) $ (a*x) `mod` m