from atcoder.math import crt def extgcd(a, b): if b == 0: return 1, 0 else: x, y = extgcd(b, a % b) return y, x - a // b * y def solve(A, B, a, b): ans, _ = crt([a, b], [A, B]) return ans def main(): A, B, a, b = map(int, input().split()) ans = solve(A, B, a, b) print(ans) if __name__ == "__main__": main()