a, b = io.read("*n", "*n") c, d = io.read("*n", "*n") i = 0 while true do if i % a == c and i % b == d then break end i = i + 1 end print(i)