a,b,c,d=gets.split.map &:to_i
(0..).each{|i|
  if i%a==c && i%b==d then
    p i
    exit
  end
}