from fractions import gcd

s = raw_input()
arr = s.split()

a = int(arr[0])
b = int(arr[1])
x = int(arr[2])
y = int(arr[3])

temp = 0
if (b > a):
	temp = x
	x = y
	y = temp
	temp = a
	a = b
	b = temp

last = 0
i, j = 0, 1
while(i < a):
	if (j%x != 0):
		last = j
		i+=1
	j+=1

# for i in arr:
# 	print i
# print "a: ", x

i, max, j = 0, 0, 1
while(i < b):
	if (j%y != 0) and (j%x == 0 or j > last):
		i+=1
		max = j
	j+=1
if (max < last):
	max = last
print(max)