import Data.List (find)
import Data.Maybe (fromJust)

main :: IO ()
main = solve <$> f >>= print
  where
    f = map read <$> words <$> getLine

solve :: [Int] -> Int
solve [aa, bb, a, b] =
  fromJust $ find f [0..]
  where
    f x = x `mod` aa == a && x `mod` bb == b