A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times *b*, *b* + *a*, *b* + 2*a*, *b* + 3*a*, ... and Morty screams at times *d*, *d* + *c*, *d* + 2*c*, *d* + 3*c*, .... 

The Monster will catch them if at any point they scream at the same time, so it wants to know when it will catch them (the first time they scream at the same time) or that they will never scream at the same time.
The first line of input contains two integers *a* and *b* (1 ≤ *a*, *b* ≤ 100). 

The second line contains two integers *c* and *d* (1 ≤ *c*, *d* ≤ 100).
In the first sample testcase, Rick's 5th scream and Morty's 8th time are at time 82. 

In the second sample testcase, all Rick's screams will be at odd times and Morty's will be at even times, so they will never scream at the same time.
20 2
9 19
82
2 1
16 12
-1
39 52
88 78
1222
59 96
34 48
1748
87 37
91 29
211
11 81
49 7
301
39 21
95 89
3414
59 70
48 54
1014
87 22
98 32
718
15 63
51 13
-1
39 7
97 91
1255
18 18
71 71
1278
46 71
16 49
209
70 11
74 27
2321
94 55
20 96
-1
18 4
77 78
1156
46 44
23 55
-1
74 88
77 37
1346
94 37
34 7
789
22 81
80 88
-1
46 30
34 62
674
40 4
81 40
364
69 48
39 9
48
89 93
84 87
5967
17 45
42 65
317
41 85
95 46
331
69 30
41 16
1410
93 74
99 93
-1
17 19
44 75
427
45 63
98 53
3483
69 11
48 34
-1
55 94
3 96
204
100 100
100 100
100
1 1
1 1
1
1 1
1 100
100
1 100
100 1
101
98 1
99 100
9703
98 1
99 2
9605
97 2
99 100
4852
3 3
3 1
-1
3 2
7 2
2
2 3
2 5
5
2 3
2 3
3
100 3
100 5
-1
6 10
12 14
-1
4 2
4 4
-1
2 3
2 2
-1
2 3
4 99
99
1 5
1 5
5
1 100
3 1
100
2 2
2 1
-1
2 10
6 20
20
2 2
2 10
10
3 7
3 6
-1
1 100
1 100
100
7 25
39 85
319
84 82
38 6
82
7 7
7 14
14
'''
# BT1
def isPrime(a):
	if a <=1:
		return False
	elif a == 2:
		return True
	else:
		for i in range (2,a):
			if a%i == 0:
				return False
				break
		return True

#print(isPrime(11))
#print(isPrime(17))

a = int(input())
b = int(input())
if isPrime(a) and isPrime(b):
	print (max(a,b))
elif isPrime(a) and not isPrime(b):
	print(a)
elif not isPrime(a) and isPrime(b):
	print(b)
else:
	print (-1)



# BT2
def summ(n):
	s = 0
	for i in range(1,n+1):
		s += i**i
	return s
 
ha = int(input())
print(summ(ha))


# BT3
def uoc(n):
	listt = []
	for i in range(1,n):
		if n%i == 0:
			listt.append(i)
	return max(listt)

a = int(input())
print(uoc(a))


# BT4
def phanso(a,b):
	listt = []
	tu = a
	mau = b
	# dcm min chu max gi!!! T.T

	somax = max(a,b)
	for i in range(2,somax+1):
		if a%i==0 and b%i == 0:
			tu = a//i
			mau = b//i
			listt.append((tu,mau))
			

	if len(listt) == 0:
		return (tu,mau)
	else:
		return(listt[-1])




a,b = map(int,input().split())
c,d = phanso(a,b)
print(c,d)


# BT5
# func return ucln, not allUoc
def allUoc(a,b):
	listuoc = []
	somax = max(a,b)
	for i in range(2,somax+1):
		if a%i == 0 and b%i == 0:
			listuoc.append(i)
	if listuoc == []:
		return(1)
	else:	
		return(max(listuoc))


def uoc(n):
	listt = []
	for i in range(1,n+1):
		if n%i == 0:
			listt.append(i)
	return max(listt)



a,b,c = map(int, input().split())
listt = [a,b,c]

# if there are 2 zeros in input, find uln of the last one
if listt.count(0) == 2:
	listt.remove(0)
	listt.remove(0)
	uln = uoc(listt[0])
elif listt.count(0) == 1:
	#phanso1 = [min(a,b,c),max(a,b,c)]
	# remove 0
	listt.remove(0)
	#phanso2 = [min(listt),max(listt)]
	uln = allUoc(max(listt), min(listt))
else:
	phanso1 = [min(listt),max(listt)]
	maxx = max(a,b,c)
	listt.remove(maxx)
	phanso2 = [min(listt), max(listt)]

	uc1 = allUoc(phanso1[0],phanso1[1])
	uc2 = allUoc(phanso2[0],phanso2[1])
	if uc1 != uc2:
		uln = allUoc(uc1,uc2)
	else:
		uln = uc1
print(uln)



# BT6
def allUoc(n):
	listt = []
	for i in range(1,n+1):
		if n%i == 0:
			listt.append(i)
	return (listt)

def isPrime(a):
	if a <=1:
		return False
	elif a == 2:
		return True
	else:
		for i in range (2,a):
			if a%i == 0:
				return False
				break
		return True
def primeList(a):   #input: list, return list of prime
	priList = []
	for i in a:
		if isPrime(i):
			priList.append(i)
	return (priList)


def breakPrime(a,primeList):
	result = {}

	for i in primeList:
		#print(i)
		#print('wtf')
		count = 0  
		while a%i == 0:
			count = count + 1
			a = a/i

		result[i] = count

	return result
def mergeDict(dict1, dict2):
	new = {}
	for k,v in dict1.items():
		if k not in dict2:
			dict2[k] = dict1[k]
		else:
			if dict1[k] > dict2[k]:
				dict2[k] = dict1[k]

	

	return dict2
		


a,b,c = map(int,input().split())


uoca = allUoc(a)
uocb = allUoc(b)
uocc = allUoc(c)


primeLista = primeList(uoca)
primeListb = primeList(uocb)
primeListc = primeList(uocc)


uocPrimea = breakPrime(a,primeLista)
uocPrimeb = breakPrime(b,primeListb)
uocPrimec = breakPrime(c,primeListc)




#new = {**uocPrimea,**uocPrimeb,**uocPrimec}
new = mergeDict(uocPrimea, uocPrimeb)
new1 = mergeDict(new,uocPrimec)
result = 1

for k,v in new1.items():
	result = result * k**v

print(result)





'''

#i = 1
#while True:
#	if i%a == 0 and i%b == 0 and i%c == 0:
#		print(i)
#		break
#	else:
#		i += 1
'''



a,b,c = map(int,input().split())

i = 1
while True:
	if i%a == 0 and i%b == 0 and i%c == 0:
		print(i)
		break
	else:
		i += 1

def ucln(a,b):
	listuoc = []
	somax = max(a,b)
	for i in range(2,somax+1):
		if a%i == 0 and b%i == 0:
			listuoc.append(i)
	if listuoc == []:
		return(1)
	else:	
		return(max(listuoc))

def bcnn(a,b):
	result = int(abs(a*b)/ucln(a,b))
	return result

re1 = bcnn(a,b)
re2 = bcnn(b,c)
re3 = bcnn(a,c)
print(re1)
print(re2)
print(re3)
ha1 = ucln(re1,re2)
ha2 = ucln(re1, re3)
ha3 = ucln(re2, re3)
print(ha1)
print(ha2)
print(ha3)
'''

'''
# BT10
def factorial(n):
	if n == 1:
		return n
	else:
		return n*factorial(n-1)

a = int(input())

summ = 0
for i in range(1,a+1):
	summ += factorial(i)
print(summ)


# BT9
a = input()
lista = list(a)

half1 = []
half2 = []
result = 'YES'
#if len(lista)%2 == 0:
for i in range(len(lista)//2):

	if lista[i] != lista[len(lista)-1-i]:
		result = 'NO'
		break
print(result)


# BT8
a = input()
lista = list(a)
listt = []


for i in reversed(lista):
	listt.append(i)
numZero = 0  
for k in listt:
	if k == '0':
		numZero += 1
	else:
		break
new = listt[numZero:]
s = ""
for i in new:
	s += i
if a == '0':
	print('0')
else:
	print(s) 



# BT7
def swap(a,b):
	temp = a
	a = b
	b = temp
	return (a,b)

a,b = map(int,input().split())
ha,he = swap(a,b)
print(ha,he)


# BT11
def sol(n):
	listt = []
	for i in range(n+1):
		b = i
		a = (n-b)**(0.5)
		#print(b)
		#print(a)
		if a - int(a) == 0:
			listt.append([int(a),b])
	return listt

#print(sol(9))
#print(sol(3))

n,m = map(int, input().split())
listn = sol(n)
listm = sol(m)
#print(listn)
#print(listm)
count = 0
if len(listn) <= len(listm):
	for item in listn:
		for item1 in listm:
			if item[0] == item1[1] and item[1] == item1[0]:
				count += 1
else:
	for item in listm:
		for item1 in listn:
			if item[0] == item1[1] and item[1] == item1[0]:
				count += 1

print(count)
'''

# BT12
a,b = map(int, input().split())
c,d = map(int, input().split())
#rick = b + i*a
#morty = d + j*c
ans = -1
if (b/a) == (d/c):  # //
	ans = -1
else:
	for j in range(d-b):
		i = ((d-b) + j*c)/a
		if i - int(i) == 0:
			ans = int(b+i*a)


print(ans)


