import sys
from collections import deque,defaultdict
import itertools
import heapq
import bisect
import math

#sys.setrecursionlimit(10 ** 9)
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
ml = lambda: map(str, input().split())
li = lambda: list(mi())
li_st = lambda: list(map(str, input().split()))
lli = lambda n: [li() for _ in range(n)]
mod = 998244353

A,B,a,b = mi()

if A>B:
    for i in range(10**8):
        num1 = i*A+a
        if (num1 - b) % B == 0:
            print(num1)
            exit()
else:
    for j in range(10**8):
        num2 = j*B+b
        if (num2-a) % A == 0:
            print(num2)
            exit()