import Control.Monad main = do xys <- replicateM 3 $ liftM (map read . words) getLine let f z [x, y] = z `mod` y == x zs = filter (\z -> all (f z) xys) [1..1000000] in print $ case zs of (z:_) -> z otherwise -> (-1)