main :: IO ()
main = do
  ss <- getLine
  let [a1,m1,a2,m2,a3,m3] = map (read :: String -> Int) . words $  ss
  if [a1,m1,a2,m2,a3,m3] == [0,0,0,0,0,0]
    then return ()
    else do
      let ans = getAns [a1,m1,a2,m2,a3,m3]
      putStrLn $ show ans
      main

getAns :: [Int] -> Int
getAns [a1,m1,a2,m2,a3,m3] = c1 `koubaisuu` c2 `koubaisuu` c3
  where c1 = getCicle 1 a1 m1 0
        c2 = getCicle 1 a2 m2 0
        c3 = getCicle 1 a3 m3 0

getCicle :: Int ->Int -> Int -> Int -> Int
getCicle 1 a m 0 = getCicle (a `mod` m) a m 1
getCicle 1 a m count = count
getCicle x a m count = getCicle (a*x `mod` m) a m (count + 1)

gojohou :: Int -> Int -> Int
gojohou x y
  | x `mod` y == 0 = y
  | otherwise  = gojohou y (x `mod` y)

koubaisuu :: Int -> Int -> Int
koubaisuu x y = (x * y) `div` (gojohou (max x y) (min x y))