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 + 2a, b + 3a, ... and Morty screams at times d, d + c, d + 2c, d + 3c, ....  [Image] 

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.


-----Input-----

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).


-----Output-----

Print the first time Rick and Morty will scream at the same time, or  - 1 if they will never scream at the same time.


-----Examples-----
Input
20 2
9 19

Output
82

Input
2 1
16 12

Output
-1



-----Note-----

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.

Source: codeforces | Difficulty: EASY
Tags: Number theory

Mathematics

Complete search


Solutions:
def compute_gcd(a, b):
	if a == 0:
		return b
	return compute_gcd(b % a, a)

def main():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	gcd = compute_gcd(a, c)
	if (b - d) % gcd != 0:
		print(-1)
	steps = 1000
	size = max(b, d) + max(a, c) * steps
	used = [False] * size
	x = b
	for _ in range(steps):
		used[x] = True
		x += a
	x = d
	for _ in range(steps):
		if used[x]:
			print(x)
			break
		x += c
main()


(a, b) = input().split()
(c, d) = input().split()
(a, b, c, d) = (int(x) for x in (a, b, c, d))
Rick = {b + a * x for x in range(1000005)}
Morty = {d + c * x for x in range(1000005)}
moments = Rick.intersection(Morty)
if len(moments) == 0:
	print(-1)
else:
	print(min(moments))


import math
import os
import random
import re
from sys import stdin, stdout
from collections import Counter

def si():
	return str(input())

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
(a, b) = mi()
(c, d) = mi()
while b != d and b < 100000.0:
	if b < d:
		b += a
	else:
		d += c
if b == d:
	print(b)
else:
	print('-1')


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
ans = -1
for i in range(100000):
	if b == d:
		ans = b
		break
	elif b < d:
		b += a
	else:
		d += c
print(ans)


(a, b) = (int(v) for v in input().split())
(c, d) = (int(v) for v in input().split())
r = {b + a * i for i in range(100)}
s = {d + c * i for i in range(100)}
i = r.intersection(s)
print(min(i) if len(i) else -1)


import sys
[a, b] = [int(x) for x in input().split(' ')]
[c, d] = [int(x) for x in input().split(' ')]
maximum = max(a, b, c, d)
for i in range(maximum + 1):
	y = (a * i + b - d) / c
	x = (c * y + d - b) / a
	answer = a * i + b
	if x.is_integer() and y.is_integer() and (answer >= max(b, d)):
		print(answer)
		sys.exit()
print(-1)


import sys
(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
if a < c:
	(b, a, d, c) = (d, c, b, a)
foundoverlap = False
x = b
while x < 100000:
	if x < d:
		x = x + a
		continue
	if (x - d) % c == 0:
		print(x)
		foundoverlap = True
		break
	x = x + a
if not foundoverlap:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
e = set(range(b, 10000, a))
f = set(range(d, 10000, c))
g = e & f
print(min(g) if g else -1)


import sys
import math

def main():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	arr = list()
	dr = 0
	for i in range(0, 202):
		arr.append(b + dr * a)
		arr.append(d + dr * c)
		dr += 1
	arr = sorted(arr)
	for i in range(0, len(arr) - 1):
		if arr[i] == arr[i + 1]:
			return print(arr[i])
	print(-1)
main()


def gcdex(a, b):
	(i, j, k, l) = (1, 0, 0, 1)
	while b > 0:
		(a, b, i, j, k, l) = (b, a % b, k, l, i - a // b * k, j - a // b * l)
	return (a, i, j)
(a1, b1) = map(int, input().split())
(c1, d1) = map(int, input().split())
a = a1
b = -c1
c = d1 - b1
(d, x, y) = gcdex(abs(a), abs(b))
if c % d == 0:
	a //= d
	b //= d
	c //= d
	x *= c
	y *= c
	x %= b
	y = (c - a * x) // b
	while x < 0 or y < 0 or int(y) != y:
		x += 1
		y = (a1 * x + b1 - d1) / c1
	print(x * a1 + b1)
else:
	print(-1)


def nsd(x, y):
	while x != 0:
		(y, x) = (x, y % x)
	return y

def nsk(x, y):
	return x * y / nsd(x, y)
(a, b) = (int(i) for i in input().split())
(c, d) = (int(i) for i in input().split())
interval = nsk(a, c)
n = 0.1
m = 0
while not (n.is_integer() and n >= 0):
	n = (b + m * a - d) / c
	m += 1
	if n > interval + d + b or m > interval + d + b:
		print(-1)
		break
if not (n > interval + d + b or m > interval + d + b):
	print(b + (m - 1) * a)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(1000 * 1000):
	if (d - b + i * c) % a == 0 and d - b + i * c > -1:
		print(d + i * c)
		exit(0)
print(-1)


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

def GCD(a, b):
	while b != 0:
		r = a % b
		a = b
		b = r
	return a
if (d - b) % GCD(a, c) != 0:
	print(-1)
else:
	y = 0
	while True:
		x = (d - b + c * y) // a
		if x >= 0 and (d - b + c * y) % a == 0:
			print(a * x + b)
			break
		y += 1


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for nr in range(1, 1001):
	for nm in range(1, 1001):
		if b + (nr - 1) * a == d + (nm - 1) * c:
			print(b + (nr - 1) * a)
			exit()
print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
r = set(range(b, 10000, a))
m = set(range(d, 10000, c))
ans = r & m
if ans:
	print(min(ans))
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
arr = [0] * 1000000
flag = 0
for i in range(100):
	arr[a * i + b] = 1
for j in range(100):
	if arr[c * j + d] == 1:
		flag = 1
		break
if flag == 0:
	print('-1')
else:
	print(c * j + d)


def solution(a, b, c, d):
	mult = 0
	seti = set()
	for i in range(10000):
		aux = b + mult * a
		if aux not in seti:
			seti.add(aux)
		else:
			return aux
		aux = d + mult * c
		if aux not in seti:
			seti.add(aux)
		else:
			return aux
		mult += 1
	return -1

def main():
	(a, b) = [int(x) for x in input().split()]
	(c, d) = [int(x) for x in input().split()]
	print(solution(a, b, c, d))
main()


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
f = 0
i = 0
for i in range(101):
	for j in range(101):
		if b + i * a == d + j * c:
			f = 1
			break
	if f == 1:
		break
if f:
	print(b + a * i)
else:
	print(-1)


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def main():
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	(c, d) = input().split()
	c = int(c)
	d = int(d)
	if abs(d - b) % gcd(max(a, c), min(a, c)):
		print(-1)
	else:
		i = 0
		while True:
			ans = b + i * a
			if ans >= d and (not (ans - d) % c):
				print(ans)
				break
			i += 1
main()


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
while b != d and b < 10 ** 5:
	if b < d:
		b += a
	else:
		d += c
if b == d:
	print(b)
else:
	print(-1)


def prog():
	(b, a) = [int(i) for i in input().split()]
	(d, c) = [int(i) for i in input().split()]
	times = []
	for i in range(1000):
		times.append(a + i * b)
	for i in range(1000):
		if c + i * d in times:
			print(c + i * d)
			exit()
	print('-1')
prog()


(a, b) = map(int, input().split())
(m, n) = map(int, input().split())
e = 9999999999
flag = True
for i in range(0, 100, 1):
	for j in range(0, 100, 1):
		if b + a * i == n + m * j:
			if b + a * i < e:
				e = b + a * i
if e != 9999999999:
	print(e)
else:
	print('-1')


(a, b) = input().split()
(c, d) = input().split()
(a, b, c, d) = (int(a), int(b), int(c), int(d))
arr = []
flag = 0
for i in range(0, 1000):
	arr.append(b)
	b = b + a
for i in range(0, 1000):
	if arr.__contains__(d):
		flag = 1
		break
	d = d + c
if flag == 1:
	print(d)
else:
	print(-1)


flag = 1
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(1, 1000):
	if flag == 1:
		for j in range(1, 1000):
			if b + a * i - a - d - c * j + c == 0:
				print(b + (i - 1) * a)
				flag = 0
				break
	else:
		break
if flag == 1:
	print('-1')


(a, b) = [int(v) for v in input().split(' ')]
(c, d) = [int(v) for v in input().split(' ')]

def gcd(x, y):
	if x == 0:
		return y
	return gcd(y % x, x)
if (d - b) % gcd(c, a) != 0:
	print(-1)
else:
	finished = False
	i = 0
	while not finished:
		j = (b + a * i - d) // c
		if b + a * i == d + c * j and j >= 0:
			print(b + a * i)
			finished = True
		i += 1


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
lcm = a * c
for i in range(10000):
	t1 = b + i * a
	x2 = (t1 - d) // c
	if t1 == d + x2 * c and x2 >= 0:
		print(t1)
		quit()
print('-1')


import sys
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(1000):
	for j in range(1000):
		if b + a * i == d + c * j:
			print(b + a * i)
			exit()
print(-1)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
(a, b) = [int(i) for i in input().split()]
(c, d) = [int(i) for i in input().split()]
g = gcd(a, c)
if g != 1 and (d - b) % gcd(a, c) != 0:
	print(-1)
	exit()
s1 = b
s2 = d
if s1 == s2:
	print(s1)
	exit()
while True:
	while s2 < s1:
		s2 += c
	if s2 == s1:
		print(s1)
		exit()
	s1 += a


(a, b) = tuple(map(int, input().split()))
(c, d) = tuple(map(int, input().split()))
from sys import exit
mod = d % c
for i in range(1000):
	if b >= d and b % c == mod:
		print(b)
		exit()
	b += a
print(-1)


ab = list(map(int, input().split()))
a = ab[0]
b = ab[1]
cd = list(map(int, input().split()))
c = cd[0]
d = cd[1]
list1 = []
list2 = []
counter1 = 0
counter2 = 0
if a + b > c + d:
	for i in range(a + b):
		list1.append(a * counter1 + b)
		counter1 += 1
	for i in list1:
		if (i - d) % c == 0 and i - d >= 0:
			print(i)
			break
		if list1.index(i) == len(list1) - 1:
			print(-1)
			break
else:
	for i in range(c + d):
		list2.append(c * counter2 + d)
		counter2 += 1
	for i in list2:
		if (i - b) % a == 0 and i - b >= 0:
			print(i)
			break
		if list2.index(i) == len(list2) - 1:
			print(-1)
			break


a = list(map(int, input().split()))
b = list(map(int, input().split()))
list1 = []
list2 = []
counter1 = 0
counter2 = 0
for i in range(100):
	list1.append(a[1] + i * a[0])
	list2.append(b[1] + i * b[0])
while True:
	if list1[counter1] == list2[counter2]:
		print(list1[counter1])
		break
	elif list1[counter1] < list2[counter2]:
		counter1 += 1
	else:
		counter2 += 1
	if counter1 == 100 or counter2 == 100:
		print(-1)
		break


import sys

def solve():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	if b < d:
		(a, c) = (c, a)
		(b, d) = (d, b)
	for i in range(c):
		if (b - d + i * a) % c == 0:
			ans = b + i * a
			print(ans)
			return
	print(-1)
solve()


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
if b == d:
	print(b)
else:
	if b < d:
		b += (d - b) // a * a
	for i in range(200):
		if (b + i * a - d) % c == 0:
			print(b + i * a)
			break
	else:
		print(-1)


def main():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	for i in range(100000):
		if i - b >= 0 and (i - b) % a == 0:
			if i - d >= 0 and (i - d) % c == 0:
				print(i)
				return
	print(-1)
main()


a = input().split()
c = input().split()
b = int(a[1])
a = int(a[0])
d = int(c[1])
c = int(c[0])
r = []
t = b - d
while not t in r:
	if t == 0:
		break
	if b < d:
		b = b + a
	else:
		d = d + c
	r = r + [t]
	t = b - d
if t == 0:
	print(b)
else:
	print(-1)


(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))
x = False
for i in range(1000):
	if (a * i + b - d) % c == 0:
		if a * i + b - d >= 0:
			x = True
			print(a * i + b)
			break
if not x:
	print(-1)


(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
arr = [b + a * i for i in range(1000)]
for i in range(1000):
	if d + c * i in arr:
		print(d + c * i)
		return
print(-1)


(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
N = 100
for i in range(N + 1):
	v1 = b + i * a
	if v1 - d >= 0:
		r = (v1 - d) % c
		if r == 0:
			print(v1)
			exit()
print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
k = 1
j = 1
l = []
l1 = 0
while k <= 100:
	r1 = b - a + a * k
	while j <= 100:
		r2 = d - c + c * j
		if r1 == r2:
			l.append(r2)
			break
		else:
			j = j + 1
	j = 1
	if len(l) > 0:
		break
	k = k + 1
if len(l) > 0:
	print(l[0])
else:
	print(-1)


def sol():
	for i in range(100):
		for j in range(100):
			if a * i + b == c * j + d:
				print(a * i + b)
				return
	print(-1)
	return
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
sol()


import math
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
g = math.gcd(a, c)
if (d - b) % g != 0:
	print(-1)
else:
	(t0, t1) = (b, d)
	while t0 != t1:
		if t0 < t1:
			t0 += a
		else:
			t1 += c
	print(t0)


def readInts():
	return [int(x) for x in input().split()]
(a, b) = readInts()
(c, d) = readInts()
l = set((a * i + b for i in range(100))) & set((c * i + d for i in range(100)))
print(min(l) if l else -1)


def gcd(a, b):
	if a < 0:
		a = -a
	if b < 0:
		b = -b
	while b != 0:
		r = a % b
		(a, b) = (b, r)
	return a
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
if (d - b) % gcd(a, c) != 0:
	print(-1)
else:
	i = 0
	while True:
		j = (b + i * a - d) // c
		if j >= 0 and b + i * a == d + j * c:
			print(b + i * a)
			break
		i += 1


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(101):
	for j in range(101):
		if b + i * a == d + c * j:
			print(d + c * j)
			exit()
print(-1)


import math
R = lambda : list(map(int, input().split()))
(a, b) = R()
(c, d) = R()
if abs(b - d) % math.gcd(a, c) != 0:
	print(-1)
	exit(0)
while b != d:
	if b < d:
		b += a
	else:
		d += c
print(b)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(10 ** 5 + 1):
	x = b + a * i
	if x >= d and (x - d) % c == 0:
		print(x)
		exit()
print(-1)


def check(a, b, c, d):
	for i in range(101):
		for j in range(101):
			if b + a * i == d + c * j:
				return b + a * i
	return -1
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
print(check(a, b, c, d))


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
e = [b]
f = [d]
g = 1
h = 0
for i in range(0, 5000):
	x = b + g * a
	e.append(x)
	y = d + g * c
	f.append(y)
	g += 1
u = set(e)
i = set(f)
y = u & i
if y:
	print(min(y))
else:
	print('-1')


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
t = max(a, b, c, d) + 1
flag = -1
for i in range(t):
	if (b + a * i - d) % c == 0 and b + a * i >= d:
		flag = b + a * i
		break
print(flag)


def input_ints():
	return list(map(int, input().split()))

def main():
	(a, b) = input_ints()
	(c, d) = input_ints()
	for t in range(10 ** 6):
		if t >= b and t >= d and ((t - b) % a == 0) and ((t - d) % c == 0):
			print(t)
			return
	print(-1)
main()


(a, b) = (int(x) for x in input().split())
(c, d) = (int(x) for x in input().split())
while b != d and b < 10 ** 4:
	if b < d:
		b += a
	else:
		d += c
if b != d:
	print(-1)
else:
	print(b)


def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
y = d - b
z = gcd(a, c)
x = abs(y) % z
if x == 0:
	(p, q) = (0, 0)
	while p * a - q * c != y:
		if p * a - q * c > y:
			q += 1
		else:
			p += 1
	print(p * a + b)
else:
	print(-1)


from math import inf
(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
if b > d:
	(a, c) = (c, a)
	(b, d) = (d, b)
r1 = (d - b) % a
r2 = c % a
k2s = [(r1 + k2 * r2) % a for k2 in range(a)]
if 0 not in k2s:
	print(-1)
else:
	print(k2s.index(0) * c + d)


(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))
if d < b:
	x = b
	b = d
	d = x
	x = a
	a = c
	c = x
k = 1
r = -1
while k <= 100005:
	n = (d - b + c * (k - 1)) / a + 1
	if n == int(n):
		n = int(n)
		r = b + a * (n - 1)
		break
	k += 1
print(r)


def BS(f, n, len):
	L = 0
	R = len - 1
	while L <= R:
		mid = (L + R) // 2
		if f[mid] == n:
			return True
		elif f[mid] < n:
			L = mid + 1
		else:
			R = mid - 1
	return False
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
f = []
s = []
ans = -1
for i in range(10000):
	ff = b + i * a
	f.append(ff)
size = len(f)
for i in range(10000):
	ss = d + i * c
	if BS(f, ss, size):
		ans = ss
		break
	s.append(ss)
print(ans)


(a, b) = [int(i) for i in input().split()]
(c, d) = [int(i) for i in input().split()]
for m in range(0, 1000000):
	if (m * c + d - b) % a == 0:
		k = (m * c + d - b) // a
		if k * a + b == m * c + d and k >= 0:
			print(k * a + b)
			break
else:
	print(-1)


import math
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
i = -1
x = 0
flag = 0
while i <= 100:
	i += 1
	x = max(math.ceil(i * (c / a) + (d - b) / a), 0)
	if b + x * a == d + i * c:
		flag = 1
		break
if flag == 1:
	print(b + x * a)
else:
	print(-1)


import sys
from fractions import gcd
(a, b) = input().split()
(c, d) = input().split()
a = int(a)
b = int(b)
c = int(c)
d = int(d)
if abs(b - d) % gcd(a, c) != 0:
	print(-1)
	exit(0)
else:
	while b != d:
		if d > b:
			b = b + a
		else:
			d = d + c
	print(b)
	exit(0)


(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
t1 = b
t2 = d
solution_found = False
for i in range(10000):
	if t1 == t2:
		print(t1)
		solution_found = True
		break
	elif t1 > t2:
		t2 += c
	else:
		t1 += a
if not solution_found:
	print(-1)


import sys
import math
import itertools
import collections

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ' '.join(map(str, arr))

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
(a, b) = mi()
(c, d) = mi()
(rick, morty) = ({b}, {d})
while b < 10001:
	b += a
	rick.add(b)
while d < 10001:
	d += c
	morty.add(d)
t = rick & morty
if len(t) == 0:
	print(-1)
else:
	print(min(list(t)))


import sys
input = sys.stdin.readline
l = input().split()
a = int(l[0])
b = int(l[1])
l = input().split()
c = int(l[0])
d = int(l[1])
if b > d:
	(b, d) = (d, b)
	(a, c) = (c, a)
for i in range(105):
	if (d - b + i * c) % a == 0:
		print(d + i * c)
		quit()
print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
ans = 1e+18
for i in range(1000):
	for j in range(1000):
		if b + a * i == d + c * j:
			ans = min(ans, b + a * i)
if ans == 1e+18:
	ans = -1
print(ans)


st = input()
i = 0
n = ''
m = ''
while st[i] != ' ':
	n = n + st[i]
	i += 1
i += 1
while i < len(st):
	m = m + st[i]
	i += 1
a = int(n)
b = int(m)
st = input()
i = 0
n = ''
m = ''
while st[i] != ' ':
	n = n + st[i]
	i += 1
i += 1
while i < len(st):
	m = m + st[i]
	i += 1
c = int(n)
d = int(m)
k = 0
for i in range(1000000):
	b1 = b + a * i
	if (b1 - d) % c == 0 and b1 - d >= 0:
		print(b1)
		k = 1
		break
if k != 1:
	print(-1)


import math

def gcdex(a, b):
	if b == 0:
		return (a, 1, 0)
	else:
		(d, x1, y1) = gcdex(b, a % b)
		return (d, y1, x1 - a // b * y1)
(n1, n2) = map(int, input().split())
(n3, n4) = map(int, input().split())
a = n1
b = -n3
c = n4 - n2
(d, x0, y0) = gcdex(abs(a), abs(b))
if c % d != 0:
	print(-1)
else:
	x0 *= c // d
	y0 *= c // d
	if a < 0:
		x0 *= -1
	if b < 0:
		y0 *= -1
	if b // d > 0:
		while x0 < 0 or y0 < 0:
			x0 += b // d
			y0 -= a // d
	if b // d < 0:
		while x0 < 0 or y0 < 0:
			x0 -= b // d
			y0 += a // d
	if b // d > 0:
		while x0 >= 0 and y0 >= 0:
			x0 -= b // d
			y0 += a // d
		x0 += b // d
		y0 -= a // d
	if b // d < 0:
		while x0 >= 0 and y0 >= 0:
			x0 += b // d
			y0 -= a // d
		x0 -= b // d
		y0 += a // d
	print(n2 + x0 * n1)


import sys
input = sys.stdin.readline

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0, hi=None):
	if hi == None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

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

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math
from bisect import bisect_left

def check(n, m, k):
	print('inside', n + (k - 1) * m, k * (k + 1) // 2, (n + (k - 1) * m) // k * k)
	if (n + (k - 1) * m) // k * k <= k * (k + 1) // 2:
		return True
	return False

def solve():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	for i in range(0, 101):
		for j in range(0, 101):
			if b + i * a == d + j * c:
				print(b + i * a)
				return
	print(-1)
for _ in range(1):
	solve()


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

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

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
(a, b) = int_array()
(c, d) = int_array()
this_set = set()
for i in range(101):
	for j in range(101):
		if b + a * i == d + c * j:
			this_set.add(b + a * i)
if len(this_set) == 0:
	print(-1)
else:
	print(min(this_set))


import sys
from math import gcd

def solve():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	if b > d:
		(t1, t2) = (a, b)
		(a, b) = (c, d)
		(c, d) = (t1, t2)
	g = gcd(a, c)
	for k in range(a // g):
		t = d - b + k * c
		if t % a == 0:
			ans = d + k * c
			print(ans)
			return
	print(-1)

def debug(x, table):
	for (name, val) in table.items():
		if x is val:
			print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
			return None
solve()


(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
check = -1
for i in range(101):
	for j in range(101):
		if b + i * a == d + j * c:
			check = 1
			print(b + i * a)
			break
	if check == 1:
		break
if check == -1:
	print('-1')


a = list(map(int, input().strip().split(' ')))
b = list(map(int, input().strip().split(' ')))
a.append(a[1])
b.append(b[1])
for i in range(20000):
	if a[2] != b[2]:
		if a[2] < b[2]:
			a[2] += a[0]
		else:
			b[2] += b[0]
	else:
		print(a[2])
		break
else:
	print('-1')


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
maxloop = 100
foundFlag = False
for i in range(0, maxloop):
	for j in range(0, maxloop):
		if b + a * i == d + c * j:
			print(b + a * i)
			foundFlag = True
			break
	if b + a * i == d + c * j:
		break
if foundFlag == False:
	print(-1)


(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
valid = [0] * 1000001
cnt = b
while cnt <= 1000000:
	valid[cnt] += 1
	cnt += a
cnt = d
while cnt <= 1000000:
	valid[cnt] += 1
	cnt += c
for i in range(1000001):
	if valid[i] == 2:
		print(i)
		break
else:
	print(-1)


def find_scream():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	for i in range(0, 1000):
		current_scream = b + a * i
		if (current_scream - d) % c == 0 and current_scream - d >= 0:
			print(current_scream)
			return
	print(-1)
	return
find_scream()


import sys
input = sys.stdin.readline
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(110):
	for j in range(110):
		if b + i * a == d + j * c:
			print(b + i * a)
			sys.exit(0)
print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(b, 10 ** 4, a):
	if i in range(d, 10 ** 4, c):
		exit(print(i))
print(-1)


(b, a) = map(int, input().split())
(d, c) = map(int, input().split())
ans = -1
ar = [a + b * i for i in range(200)]
br = [c + d * i for i in range(200)]
i = 0
j = 0
while i < 200 and j < 200:
	if ar[i] == br[j]:
		ans = ar[i]
		break
	elif ar[i] < br[j]:
		i += 1
	else:
		j += 1
print(ans)


(a, b) = [int(i) for i in input().split(' ')]
(c, d) = [int(i) for i in input().split(' ')]
d1 = d % c
ans = -1
b -= a
for i in range(c + 120):
	b += a
	if b % c == d1 and b >= d:
		ans = b
		break
print(ans)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
count = 0
found = False
j = 0
while count < a:
	i = (d - b + c * j) // a
	if i >= 0:
		count += 1
		if (d - b + c * j) % a == 0:
			found = True
			break
	j += 1
if found:
	print(b + a * i)
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
res = -1
for i in range(10000):
	if (b + a * i - d) % c == 0 and b + a * i - d >= 0:
		res = b + a * i
		break
print(res)


I = lambda : map(int, input().split())
(a, b) = I()
(c, d) = I()
for p in range(d, 10 ** 5, c):
	if p in range(b, 10 ** 5, a):
		print(p)
		break
else:
	print(-1)


def calculateTime(a, b, c, d):
	ans1 = b
	ans2 = d
	i = 0
	for i in range(1000):
		for j in range(1000):
			if b + i * a == d + j * c:
				return b + i * a
				break
	return -1
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
print(calculateTime(a, b, c, d))


l = input().split()
s = input().split()
a = int(l[0])
b = int(l[1])
c = int(s[0])
d = int(s[1])
t = []
p = []
f = 1
i = 0
while i < 1000:
	t.append(b + i * a)
	p.append(d + i * c)
	if b + i * a in p:
		print(b + i * a)
		f = 0
		break
	if d + i * c in t:
		print(d + i * c)
		f = 0
		break
	i += 1
if f:
	print('-1')


(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
for i in range(100):
	if (a * i + b - d) % c == 0 and (a * i + b - d) // c >= 0:
		print(a * i + b)
		break
else:
	print(-1)


inputBuffer = input().split()
a = int(inputBuffer[0])
b = int(inputBuffer[1])
inputBuffer = input().split()
c = int(inputBuffer[0])
d = int(inputBuffer[1])
ans = 1000000000.0
for i in range(1000):
	for j in range(1000):
		if a * i + b == c * j + d:
			ans = min(ans, a * i + b)
if ans == 1000000000.0:
	ans = -1
print(ans)


def main():
	(a, b) = [int(x) for x in input().split()]
	(c, d) = [int(x) for x in input().split()]
	check = 0
	for i in range(100):
		y = b + a * i
		x = (b + a * i - d) * 1.0 / c
		if x >= 0 and x == int(x):
			print(y)
			check += 1
			break
	if check == 0:
		print('-1')
main()


(a, b) = map(int, input().strip().split())
(c, d) = map(int, input().strip().split())
flag = 0
for i in range(0, 100):
	for j in range(0, 100):
		if b + a * i == d + c * j:
			flag = 1
			ans = i
			break
		elif b + a * i < d + c * j:
			break
	if bool(flag):
		break
if bool(flag):
	print(ans * a + b)
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
same_time = 0
coeff = 0
count = 0
if (a % 2 == 0 and b % 2 != 0) and c % 2 == 0 and (d % 2 == 0):
	print('-1')
elif (a % 2 == 0 and b % 2 == 0) and c % 2 == 0 and (d % 2 != 0):
	print('-1')
elif b == d:
	print(b)
else:
	if b > d:
		g = a
		h = b
		i = c
		j = d
	else:
		g = c
		h = d
		i = a
		j = b
	new_gh = h
	new_ij = j
	while True:
		if new_gh / new_ij == 1:
			print(new_gh)
			break
		if new_gh > new_ij:
			new_ij += i
		if new_gh < new_ij:
			new_gh += g
		count += 1
		if count == 10000:
			print(-1)
			break


rick = [bool(0)] * 100001
morty = [bool(0)] * 100001
(a, b) = input().split(' ')
a = int(a)
b = int(b)
(c, d) = input().split(' ')
c = int(c)
d = int(d)
i = int(1)
krick = -1
rick_krick = b
morty_krick = d
while rick_krick <= 10000:
	rick[rick_krick] = 1
	rick_krick += a
while morty_krick <= 10000:
	morty[morty_krick] = 1
	morty_krick += c
while krick == -1 and i <= 10000:
	if morty[i] == 1 and rick[i] == 1:
		krick = i
	i += 1
print(krick)


import sys
(a, b) = (int(x) for x in input().split())
(c, d) = (int(x) for x in input().split())
for i in range(b, 2 * a * b * c * d + 1, a):
	if i - d >= 0 and (i - d) % c == 0:
		print(i)
		sys.exit(0)
print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
ONE = set()
TWO = set()
for i in range(b, 50000, a):
	ONE.add(i)
for i in range(d, 50000, c):
	TWO.add(i)
opt = 99999
for i in ONE:
	if i in TWO:
		opt = min(opt, i)
if opt == 99999:
	print(-1)
else:
	print(opt)


(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
k = -1
for i in range(max(b, d), a * c + max(b, d)):
	if (i - b) % a == 0 and (i - d) % c == 0:
		k = i
		break
print(k)


def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
A = d // a
D = d % a
C = b // c
B = b % c
g = gcd(a, c)
aa = a // g
cc = c // g
delta = D - B
if delta % g != 0:
	print(-1)
else:
	delta //= g
	for x in range(-A, -A + cc + 100):
		done = False
		for y in range(-C, -C + aa + 100):
			if aa * x - cc * y == delta:
				print(b + g * aa * (x + A))
				done = True
				break
		if done:
			break
	else:
		print(-1)


ab = list(map(int, input().split()))
a = ab[0]
b = ab[1]
cd = list(map(int, input().split()))
c = cd[0]
d = cd[1]
num_list = []
if b == d:
	print(b)
elif a > c:
	for i in range(max(c, d // a + 1)):
		num_list.append(a * i + b)
	for i in num_list:
		if (i - d) % c == 0 and i - d >= 0:
			print(i)
			break
		if num_list.index(i) == len(num_list) - 1:
			print(-1)
			break
elif a < c:
	for i in range(max(a, b // c + 1)):
		num_list.append(c * i + d)
	for i in num_list:
		if (i - b) % a == 0 and i - b >= 0:
			print(i)
			break
		if num_list.index(i) == len(num_list) - 1:
			print(-1)
			break
elif a == c:
	if (max(b, d) - min(b, d)) % a == 0:
		print(max(b, d))
	else:
		print(-1)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
from sys import stdin
(a, b) = map(int, stdin.readline().split())
(c, d) = map(int, stdin.readline().split())
const = b - d + c - a
if const % gcd(a, c) == 0:
	for m in range(1, 10 ** 7):
		if m * c >= const:
			if (m * c - const) / a == (m * c - const) // a:
				print(b + ((m * c - const) // a - 1) * a)
				break
else:
	print(-1)


import math
inp = lambda : map(int, input().split())
(a, b) = inp()
(c, d) = inp()
for i in range(10000):
	if (i * c + d - b) % a == 0 and (i * c + d - b) // a >= 0:
		print(i * c + d)
		quit()
print(-1)


import sys
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(101):
	sum1 = b + a * i
	sum2 = (sum1 - d) % c
	if sum2 == 0 and sum1 - d >= 0:
		print(sum1)
		sys.exit()
print(-1)


def gcd(a, b):
	while b != 0:
		r = a % b
		a = b
		b = r
	return a
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
if (d - b) % gcd(a, c) != 0:
	print(-1)
	exit()
y = 0
while True:
	x = (d - b + c * y) // a
	if x >= 0 and (d - b + c * y) % a == 0:
		print(d + c * y)
		break
	y += 1


(a, b) = [*map(int, input().split())]
(c, d) = [*map(int, input().split())]
e = []
count = 0
for i in range(0, 100):
	e.append(b + a * i)
for j in range(0, 100):
	ans = d + c * j
	if ans in e:
		count += 1
		ans1 = ans
		break
if count == 1:
	print(ans1)
else:
	print(-1)


from time import time as tm
(b, a) = list(map(int, input().split()))
(d, c) = list(map(int, input().split()))
cur = ma = mb = 0
stm = tm()
while tm() - stm < 0.9:
	isa = a + b * ma == cur
	isb = c + d * mb == cur
	if isa:
		ma += 1
	if isb:
		mb += 1
	if isa and isb:
		print(cur)
		break
	cur += 1
else:
	print(-1)


(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
yes = True
for i in range(1, 100):
	if a % i == c % i and c % i == 0:
		if d % i != b % i:
			yes = False
if not yes:
	print(-1)
else:
	s = set()
	for i in range(1000):
		s.add(b)
		b += a
	for i in range(1000):
		if d in s:
			print(d)
			break
		d += c


import sys
(a, b) = map(int, sys.stdin.readline().strip().split(' '))
(c, d) = map(int, sys.stdin.readline().strip().split(' '))
flag = False
maximo = max([a, b, c, d]) + 1
for i in range(maximo):
	auxK = b + i * a
	if auxK - d >= 0:
		if (auxK - d) % c == 0:
			flag = True
			break
if flag:
	print(auxK)
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
x = y = 0
chk = set([])
for i in range(0, 700000, 1):
	x = b + a * i
	y = d + c * i
	if x == y or (x in chk and y in chk):
		print(min(x, y))
		exit()
	if x in chk:
		print(x)
		exit()
	if y in chk:
		print(y)
		exit()
	chk.add(x)
	chk.add(y)
print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
r = set()
now = b
while now < d:
	now += a
d %= c
while True:
	if now % c == d:
		print(now)
		return
	if now % c in r:
		print(-1)
		return
	r.add(now % c)
	now += a


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
s1 = set()
s2 = set()
f = False
b -= a
d -= c
for i in range(10 ** 4):
	b += a
	s1.add(b)
	d += c
	s2.add(d)
	if b in s2:
		print(b)
		f = True
		break
	if d in s1:
		print(d)
		f = True
		break
if not f:
	print(-1)


import sys, math, io, os, time, itertools, collections
mod = 10 ** 9 + 7
sys.setrecursionlimit(10000)
i = sys.stdin.readline
p = sys.stdout.write
global start, end

def isin(l, x):
	left = 0
	right = len(l) - 1
	if x < l[0]:
		return -1
	while left <= right:
		mid = left + (right - left) // 2
		if l[mid] == x:
			return mid
		elif l[mid] < x:
			ans = mid
			left = mid + 1
		else:
			right = mid - 1
	return ans

def ispal(l):
	n = len(l)
	for i in range(n // 2 + 1):
		if l[i] != l[n - i - 1]:
			return False
	return True

def ccarray(l):
	d = {l[k]: k for k in range(len(l))}
	m = sorted(d)
	return [d[m[k]] for k in range(len(l))]

def is_prime(n):
	if n <= 3:
		return n > 1
	if n % 2 == 0 or n % 3 == 0:
		return False
	k = 5
	while k ** 2 <= n:
		if n % k == 0 or n % (k + 2) == 0:
			return False
		k += 6
	return True

def sieve(n):
	prime = [True for k in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for k in range(p * p, n + 1, p):
				prime[k] = False
		p += 1

def main():
	(a, b) = [int(k) for k in i().split()]
	(c, d) = [int(k) for k in i().split()]
	ans = 0
	if b >= d:
		for k in range(c):
			temp = b - d + k * a
			if temp % c == 0:
				p(str(b + a * k) + '\n')
				break
		else:
			p('-1\n')
	else:
		for k in range(a):
			temp = d - b + k * c
			if temp % a == 0:
				p(str(d + c * k) + '\n')
				break
		else:
			p('-1\n')
t = 1
start = time.perf_counter()
for _ in range(t):
	main()
end = time.perf_counter()


def inputIntegerArray():
	return list(map(int, input().split(' ')))
(a, b) = inputIntegerArray()
(c, d) = inputIntegerArray()
values = []
for i in range(0, 100):
	values.append(b + i * a)
for i in range(0, 100):
	if d + i * c in values:
		print(d + i * c)
		quit()
print('-1')


def GCD(a, b):
	while b != 0:
		r = a % b
		a = b
		b = r
	return a
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())

def solve(a, b, c, d):
	for i in range(0, 101):
		j = (b + a * i - d) // c
		if 0 <= j and b + a * i == d + c * j:
			return b + a * i
	return -1
print(solve(a, b, c, d))


import math
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
hasAnswer = 0
for i in range(101):
	y = b + i * a
	z = (y - d) / c
	if math.floor(z) == z and z >= 0:
		hasAnswer = 1
		break
if hasAnswer:
	print(y)
else:
	print(-1)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
morty = set()
cr = b
for i in range(1000):
	morty.add(cr)
	cr += a
cr = d
for i in range(1000):
	if cr in morty:
		print(cr)
		exit(0)
	cr += c
print(-1)


def main():
	(a, b) = [int(x) for x in input().split()]
	(c, d) = [int(x) for x in input().split()]
	rick = b
	morty = a
	for i in range(100):
		for j in range(100):
			if b + i * a == d + j * c:
				print(b + i * a)
				return
	print(-1)
	return
main()


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
x = b
y = d
ok = 0
aa = [0] * 11111111
for i in range(100000):
	aa[x] = 1
	x += a
for i in range(100000):
	if aa[y] == 1:
		ok = 1
		print(y)
		break
	y += c
if ok == 0:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for x in range(101):
	y = (b + a * x - d) * 1.0 / c
	if y % 1 == 0 and y >= 0:
		print(b + a * x)
		exit()
print('-1')


def gcd(a, c):
	if c == 0:
		return a
	return gcd(c, a % c)
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
f = gcd(max(a, c), min(a, c))
if (b - d) % f != 0:
	print(-1)
else:
	i = 0
	j = 0
	s1 = b
	s2 = d
	while True:
		if s1 > s2:
			s2 = d + i * c
			i += 1
		elif s2 > s1:
			s1 = b + j * a
			j += 1
		else:
			print(s1)
			break


(a, b) = [int(x) for x in input().strip().split(' ')]
(c, d) = [int(x) for x in input().strip().split(' ')]
R = set([])
M = set([])
for i in range(10000):
	R.add(b + a * i)
	M.add(d + c * i)
t = R & M
if len(t) > 0:
	print(min(t))
else:
	print(-1)


R = lambda : map(int, input().split())
(a, b) = R()
(c, d) = R()
s = set(range(b, 100 ** 2, a)) & set(range(d, 100 ** 2, c))
print(min(s) if s else -1)


import sys
I = lambda : list(map(int, input().split()))
(a, b) = I()
(c, d) = I()
for i in range(101):
	for j in range(101):
		if b + a * i == d + c * j:
			print(b + a * i)
			sys.exit()
print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
if b == d:
	print(b)
else:
	s = set()
	for i in range(0, 101):
		s.add(b + a * i)
	for i in range(0, 101):
		temp = d + c * i
		if temp in s:
			print(temp)
			exit(0)
	print(-1)


def de(x, y):
	if y % 2 != 0 and x % 2 == 0:
		k = 'odd'
	elif y % 2 == 0 and x % 2 == 0:
		k = 'even'
	else:
		k = 'undef'
	return k

def uc(m, n):
	for i in range(min(m, n) + 1, 0, -1):
		if m % i == 0 and n % i == 0:
			break
	return i
(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))
f = 0
if de(a, b) != 'undef' and de(c, d) != 'undef' and (de(a, b) != de(c, d)) or abs(d - b) % uc(a, c) != 0:
	print(-1)
else:
	while True:
		if a >= c:
			if (b + f * a - d) % c == 0 and b + f * a - d >= 0:
				print(b + f * a)
				break
			else:
				f += 1
		elif (d + f * c - b) % a == 0 and d + f * c - b >= 0:
			print(d + f * c)
			break
		else:
			f += 1


(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
i = 0
j = 0
flag = 0
for i in range(1000000):
	s1 = b + a * i
	if s1 >= d and 0 == (s1 - d) % c:
		print(s1)
		flag = 1
		break
if not flag:
	print('-1')


(a, b) = list(map(int, input().split(' ')))
(c, d) = list(map(int, input().split(' ')))
final = -1
for i in range(101):
	soma = b + a * i
	for j in range(101):
		soma2 = d + c * j
		if soma2 == soma:
			final = soma2
			break
		if soma2 > soma:
			break
	if final > -1:
		break
print(final)


(b, a) = map(int, input().split())
(d, c) = map(int, input().split())
if a < c:
	(a, b, c, d) = (c, d, a, b)
R = [a + i * b for i in range(d) if (a + i * b - c) % d == 0]
if R:
	print(min(R))
else:
	print(-1)


def main():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	s1 = set([b + t * a for t in range(200)])
	s2 = set([d + t * c for t in range(200)])
	ans = float('inf')
	for x in s1:
		if x in s2:
			ans = min(ans, x)
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)
main()


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
n = 0
m = 0
while n < 10000 and m < 10000:
	abn = b + a * n
	cdm = d + c * m
	if abn < cdm:
		n += 1
	elif abn > cdm:
		m += 1
	else:
		break
if abn == cdm:
	print(abn)
else:
	print('-1')


import collections
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
v = collections.defaultdict(int)
i = 0
while True:
	if b - d > 0:
		if (b - d + a * i) % c == 0:
			print(b + a * i)
			break
		if v[(b - d + a * i) % c] not in v:
			v[(b - d + a * i) % c] += 1
		else:
			print(-1)
			break
	else:
		if (d - b + c * i) % a == 0:
			print(d + i * c)
			break
		if v[(d - b + c * i) % a] not in v:
			v[(d - b + c * i) % a] += 1
		else:
			print(-1)
			break
	i += 1


from sys import maxsize, stdout, stdin, stderr
tup = lambda : map(int, stdin.readline().split())
I = lambda : int(stdin.readline())
lint = lambda : [int(x) for x in stdin.readline().split()]
S = lambda : stdin.readline().replace('\n', '').strip()

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
stpr = lambda x: stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
from collections import defaultdict, Counter
from math import gcd
from bisect import bisect_left
from math import floor, ceil
(a, b) = tup()
(c, d) = tup()
for i in range(1, 101):
	f1 = b + (i - 1) * a
	for j in range(1, 101):
		f2 = d + (j - 1) * c
		if f2 == f1:
			print(f2)
			exit()
print('-1')


from fractions import gcd
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
val = gcd(a, c)
if (d - b) % val != 0:
	print(-1)
else:
	i = b
	while True:
		if (i - d) % c == 0 and (i - d) / c >= 0:
			break
		i = i + a
	print(i)


from math import *
(a, b) = map(int, input().split())
(c, d) = list(map(int, input().split()))
x = b - d
if x % gcd(a, c) != 0:
	print(-1)
else:
	y = -1
	for i in range(101):
		for j in range(101):
			if a * i - c * j == d - b:
				y = i
				break
		if y >= 0:
			break
	if y < 0:
		print(-1)
	else:
		print(a * i + b)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for x in range(100):
	for y in range(100):
		if b + x * a == d + y * c:
			print(b + x * a)
			quit()
print(-1)


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

def get_answer():
	for i in range(101 * 2):
		for j in range(101 * 2):
			if b + a * i == d + c * j:
				return b + a * i
	return -1
print(get_answer())


def greatestCommonDivisor(a, b):
	if a == 0 or b == 0:
		return a + b
	while a != b:
		if a > b:
			a -= b
		else:
			b -= a
	return a

def firstScreamTime(a, b, c, d):
	bias = d - b
	gcd = greatestCommonDivisor(a, c)
	if bias % gcd != 0:
		return -1
	y = 0
	while True:
		x = (bias + c * y) / a
		if x == x // 1 and x >= 0:
			return y * c + d
		y += 1
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
print(firstScreamTime(a, b, c, d))


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
t = 10 ** 5
i = 0
flag = 0
if d > b:
	while i < t:
		r = d - b + c * i
		if r % a == 0:
			flag = 1
			break
		i = i + 1
	if flag == 0:
		print(-1)
		exit()
	else:
		print(d + c * i)
else:
	while i < t:
		r = b - d + a * i
		if r % c == 0:
			flag = 1
			break
		i = i + 1
	if flag == 0:
		print(-1)
	else:
		print(a * i + b)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
t = 0
for i in range(1000):
	m = (b - d + a * i) / c
	u = m - int(m)
	if u == 0 and m >= 0:
		print(d + c * int(m))
		t = 1
		break
if t == 0:
	print('-1')


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(b, 100000, a):
	if i in range(d, 100000, c):
		exit(print(i))
print(-1)


from collections import deque as de
import math
from collections import Counter as cnt
from functools import reduce
from typing import MutableMapping

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

class My_stack:

	def __init__(self):
		self.data = []

	def my_push(self, x):
		return self.data.append(x)

	def my_pop(self):
		return self.data.pop()

	def my_peak(self):
		return self.data[-1]

	def my_contains(self, x):
		return self.data.count(x)

	def my_show_all(self):
		return self.data

	def isEmpty(self):
		return len(self.data) == 0
arrStack = My_stack()

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def get_prime_factors(number):
	prime_factors = []
	while number % 2 == 0:
		prime_factors.append(2)
		number = number / 2
	for i in range(3, int(math.sqrt(number)) + 1, 2):
		while number % i == 0:
			prime_factors.append(int(i))
			number = number / i
	if number > 2:
		prime_factors.append(int(number))
	return prime_factors

def get_frequency(list):
	dic = {}
	for ele in list:
		if ele in dic:
			dic[ele] += 1
		else:
			dic[ele] = 1
	return dic

def Log2(x):
	return math.log10(x) / math.log10(2)

def isPowerOfTwo(n):
	return math.ceil(Log2(n)) == math.floor(Log2(n))
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
dic = {}
dic[b] = 1
for i in range(1, 100000):
	dic[b + a * i] = 1
ch = 1
for i in range(100000):
	if d + c * i in dic:
		print(d + c * i)
		ch = 0
		break
if ch:
	print(-1)


i = 1
yes = -1
y = 0
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
if b == d:
	print(d)
else:
	for j in range(100):
		i = (j * c + d - b) / a
		if i - int(i) == 0 and i >= 0:
			yes = i
		if yes != -1:
			break
	if yes == -1:
		print(-1)
	else:
		y = yes * a + b
		print(int(y))


from sys import stdin, stdout
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import *
from random import *
input = stdin.readline
prin = stdout.write
from random import sample
from collections import Counter, deque
from fractions import *
from math import sqrt, ceil, log2, gcd, cos, pi
mod = 10 ** 9 + 7

def f(arr, i, j, d, dist):
	if i == j:
		return
	nn = max(arr[i:j])
	for tl in range(i, j):
		if arr[tl] == nn:
			dist[tl] = d
			f(arr, i, tl, d + 1, dist)
			f(arr, tl + 1, j, d + 1, dist)

def ps(n):
	cp = 0
	lk = 0
	arr = {}
	while n % 2 == 0:
		n = n // 2
		lk += 1
	for ps in range(3, ceil(sqrt(n)) + 1, 2):
		lk = 0
		cc = 0
		while n % ps == 0:
			n = n // ps
			lk += 1
	if n != 1:
		lk += 1
		return False
	return True

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def factorials(n, r):
	slr = n
	dpr = r
	qlr = 1
	qs = 1
	mod = 10 ** 9 + 7
	for ip in range(n - r + 1, n + 1):
		qlr = qlr * ip % mod
	for ij in range(1, r + 1):
		qs = qs * ij % mod
	ans = qlr * modInverse(qs) % mod
	return qlr

def modInverse(b):
	qr = 10 ** 9 + 7
	return pow(b, qr - 2, qr)
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def power(arr):
	listrep = arr
	subsets = []
	for i in range(2 ** len(listrep)):
		subset = []
		for k in range(len(listrep)):
			if i & 1 << k:
				subset.append(listrep[k])
		subsets.append(subset)
	return subsets

def pda(n):
	list = []
	su = 0
	for i in range(1, int(sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				list.append(i)
				su += i
			else:
				list.append(n // i)
				list.append(i)
				su += i
				su += n // i
	return su

def dis(xa, ya, xb, yb):
	return sqrt((xa - xb) ** 2 + (ya - yb) ** 2)

def ii():
	return int(input())

def ilist():
	return [int(x) for x in input().strip().split()]

def islist():
	return list(map(str, input().split().rstrip()))

def inp():
	return input().strip()

def google(test, ans):
	return 'Case #' + str(test) + ': ' + str(ans)

def overlap(x1, y1, x2, y2):
	if x2 > y1:
		return y1 - x2
	if y1 > y2:
		return y2 - x2
	return y1 - x2

def dist(x1, y1, x2, y2):
	return sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
t = 1
for p in range(t):
	(a, b) = ilist()
	(c, d) = ilist()
	cc = 0
	ans = -1
	for k in range(101):
		for p in range(101):
			if d + c * k == b + a * p:
				print(b + a * p)
				cc = 1
				break
		if cc == 1:
			break
	if cc == 0:
		print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
flag = 0
for i in range(1, 10 ** 4):
	t1 = (i - b) / a
	t2 = (i - d) / c
	if int(t1) == t1 and int(t2) == t2 and (t1 >= 0) and (t2 >= 0):
		print(i)
		flag = 1
		break
if flag == 0:
	print(-1)


from fractions import gcd
import time
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
gap = abs(b - d)
if gap % gcd(a, c) != 0:
	print(-1)
	exit()
while True:
	if b < d:
		r = (d - b) % a
		if r == 0:
			print(d)
			exit(0)
		b = d + (a - r)
	else:
		r = (b - d) % c
		if r == 0:
			print(b)
			exit(0)
		d = b + (c - r)


(a, b) = [int(i) for i in input().split()]
(c, d) = [int(i) for i in input().split()]
for i in range(0, 10000):
	if b + a * i >= d and (b + a * i - d) % c == 0:
		print(b + a * i)
		return
print(-1)


(a, b) = input().split(' ')
(c, d) = input().split(' ')
a = int(a)
b = int(b)
c = int(c)
d = int(d)
s = b
s1 = d
j = 0
if not a & a - 1 == a - 1 and (not c & c - 1 == c - 1):
	if b & b - 1 == b - 1 and (not d & d - 1 == d - 1):
		print('-1')
		j = 1
while j == 0 and s < 100000:
	if s == s1:
		print(s)
		j = 1
	elif s1 > s:
		s = s + a
	else:
		s1 = s1 + c
if j == 0:
	print('-1')


from sys import stdin, stdout
(a, b) = map(int, stdin.readline().rstrip().split())
(c, d) = map(int, stdin.readline().rstrip().split())
match = -1
set1 = set()
for i in range(a + b + max([b, d])):
	set1.add(b + i * a)
for i in range(a + b + max([b, d])):
	if d + i * c in set1:
		match = d + i * c
		break
print(match)


from fractions import gcd
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
g = gcd(a, c)
if (d - b) % g == 0:
	big_tab = [0 for _ in range(101 * 101)]
	for i in range(b, 101 * 101, a):
		big_tab[i] = 1
	for i in range(d, 101 * 101, c):
		if big_tab[i]:
			print(i)
			break
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
sum1 = b
sum2 = d
stop = False
numLoop = 0
while sum1 != sum2 and numLoop < 10 ** 6:
	numLoop += 1
	if sum1 < sum2:
		sum1 += a
	else:
		sum2 += c
if sum1 == sum2:
	stop = True
if stop == True:
	print(sum1)
else:
	print(-1)


def R():
	return map(int, input().split())

def I():
	return int(input())

def S():
	return str(input())

def L():
	return list(R())
from collections import Counter
import math
import sys
from itertools import permutations
import bisect
(b, a) = R()
(d, c) = R()
for k in range(1000000):
	if a + b * k - c >= 0 and (a + b * k - c) % d == 0:
		print(a + b * k)
		exit()
print('-1')


(a, b) = (int(i) for i in input().split())
(c, d) = (int(i) for i in input().split())
br = -1
for k in range(1000000):
	n = (d - b + c * k) / a
	if n >= 0 and (not n % 1):
		br = k
		break
if br != -1:
	print(d + c * br)
else:
	print(-1)


import sys
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
li = []
li1 = []
for i in range(100):
	for j in range(100):
		if b + a * i == d + c * j:
			print(b + a * i)
			sys.exit()
print('-1')


(rick_a, rick_b) = map(int, input().split())
(morty_a, morty_b) = map(int, input().split())
_arr_rick = []
_arr_morty = []
_arr_rick.append(rick_b)
_arr_morty.append(morty_b)
_res = False
for x in range(1, 101):
	_rick_cord = rick_b + x * rick_a
	_morty_cord = morty_b + x * morty_a
	_arr_rick.append(_rick_cord)
	_arr_morty.append(_morty_cord)
for x in _arr_rick:
	for y in _arr_morty:
		if x == y:
			print(x)
			_res = True
			break
	if _res:
		break
if not _res:
	print(-1)


import sys
(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
(cur1, cur2) = (b, d)
x = set()
y = set()
for i in range(100000):
	x.add(cur1)
	y.add(cur2)
	cur1 += a
	cur2 += c
for i in range(100000):
	if i in x and i in y:
		print(i)
		return
print(-1)


(a, b) = [int(i) for i in input().split()]
(c, d) = [int(i) for i in input().split()]
ans = -1
for i in range(int(1000000.0)):
	if b < d:
		b += a
	elif d < b:
		d += c
	else:
		ans = d
		break
print(ans)


import sys
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
s = set()
for i in range(1001):
	s.add(b + i * a)
for i in range(1000):
	num = d + c * i
	if num in s:
		print(num)
		sys.exit()
print(-1)


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

def rick(a, b, r):
	return b + a * r

def morty(c, d, r):
	return d + c * r

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
pos = d - b
if pos % gcd(a, c) != 0:
	print(-1)
else:
	i = 0
	j = 0
	while True:
		if rick(a, b, i) < morty(c, d, j):
			i += 1
		elif rick(a, b, i) > morty(c, d, j):
			j += 1
		else:
			print(rick(a, b, i))
			break


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
p = True
for i in range(max(b, d), 1000000):
	if (i - d) % c == 0 and (i - b) % a == 0:
		print(i)
		p = False
		break
if p:
	print(-1)


gcd = lambda a, b: gcd(b, a % b) if b else a
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
if (d - b) % gcd(a, c):
	print(-1)
else:
	while True:
		if b == d:
			print(b)
			break
		if b < d:
			b += a
		else:
			d += c


(a, b) = input().split()
a = int(a)
b = int(b)
(c, d) = input().split()
c = int(c)
d = int(d)
i = 0
j = 0
ric = []
morti = []
while i <= 100:
	ric.append(a * i + b)
	i += 1
while j <= 100:
	morti.append(c * j + d)
	j += 1
ric = set(ric)
morti = set(morti)
answ = list(ric.intersection(morti))
answ.sort()
if len(answ) == 0:
	print(-1)
else:
	print(answ[0])


(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))
flag = 0
rick = []
morty = []
for i in range(101):
	rick.append(b + i * a)
	morty.append(d + i * c)
for i in range(len(rick)):
	if rick[i] in morty:
		print(rick[i])
		break
	else:
		flag += 1
if flag == len(rick):
	print(-1)


k = [int(x) for x in input().strip().split()]
a = k[0]
b = k[1]
m = [int(x) for x in input().strip().split()]
c = m[0]
d = m[1]
out = -1
while d < 10 ** 6 or b < 10 ** 6:
	if b > d:
		d += c
	elif d > b:
		b += a
	else:
		out = b
		break
print(out)


import sys

def diff(a, b):
	return abs(a - b)
line1 = input()
line2 = input()
interval1 = int(line1.split()[0])
offset1 = int(line1.split()[1])
interval2 = int(line2.split()[0])
offset2 = int(line2.split()[1])
diff_set = set()
if offset1 <= offset2:
	scream1 = offset1
	scream2 = offset2
	int1 = interval1
	int2 = interval2
else:
	scream1 = offset2
	scream2 = offset1
	int1 = interval2
	int2 = interval1
while scream1 != scream2:
	d = diff(scream1, scream2)
	if d in diff_set:
		print(-1)
		sys.exit(0)
	else:
		diff_set.add(d)
	while scream2 > scream1:
		scream1 += int1
	while scream1 > scream2:
		scream2 += int2
print(scream1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
i = 0
j = 0
high = max(a, b, c, d)
ans = -1
while i <= high and j <= high:
	if i * a + b == j * c + d:
		ans = i * a + b
		break
	elif i * a + b > j * c + d:
		j += 1
	else:
		i += 1
print(ans)


import sys
import collections
from collections import Counter
import itertools
import math
import timeit

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
(a, b) = mi()
(c, d) = mi()
for i in range(101):
	for j in range(101):
		if b + i * a == d + j * c:
			exit(print(b + i * a))
print(-1)


def function(a, b, c, d):
	l1 = []
	l2 = []
	for i in range(1, 1000):
		eq1 = b + a * i - a
		eq2 = d + c * i - c
		if eq1 not in l1:
			l1.append(eq1)
		if eq2 not in l2:
			l2.append(eq2)
		if eq1 in l2:
			return eq1
		if eq2 in l1:
			return eq2
	return -1
(a, b) = map(int, input().rstrip().split())
(c, d) = map(int, input().rstrip().split())
print(function(a, b, c, d))


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
fl = 0
if b == d:
	print(b)
else:
	for m in range(1, 10010):
		if (c * m + d - c - b) % a == 0 and (c * m + d - c - b) // a + 1 > 0:
			print(c * m + d - c)
			fl = 1
			break
	if fl == 0:
		print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
x = d - b
m = -1
n = 0
if x % a == 0:
	while True:
		m = m + 1
		n = (x + m * c) / a
		if n < 0:
			continue
		if (x + m * c) % a == 0:
			print(d + m * c)
			break
elif x % a != 0 and c % a == 0:
	print(-1)
else:
	while m <= a:
		m = m + 1
		if (x % a + c % a * m) % a == 0:
			print(d + m * c)
			exit()
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
t = []
for n1 in range(1, 101):
	for n2 in range(1, 101):
		if b + (n1 - 1) * a == d + (n2 - 1) * c:
			t.append(b + (n1 - 1) * a)
if len(t) != 0:
	print(min(t))
else:
	print(-1)


a = list(map(int, input().split()))
b = list(map(int, input().split()))
p = 0
for i in range(1000):
	for j in range(1000):
		if a[1] + i * a[0] == b[1] + j * b[0]:
			count = a[1] + i * a[0]
			p = 1
			break
	if p == 1:
		break
if p == 1:
	print(count)
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
ans = None
for i in range(b, 100 ** 2, a):
	if i in range(d, 100 ** 2, c):
		ans = i
		break
if ans == None:
	ans = -1
print(ans)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
rik = [b + x * a for x in range(500)]
mor = [d + x * c for x in range(500)]
for r in rik:
	if r in mor:
		print(r)
		break
else:
	print(-1)


R = lambda : map(int, input().split())
(a, b) = R()
(c, d) = R()
n = 100000
f = [0] * n
i = b
while i < 10000:
	f[i] |= 1
	i += a
i = d
while i < 10000:
	f[i] |= 2
	i += c
found = False
i = 0
while i < 10000:
	if f[i] == 3:
		print(i)
		found = True
		break
	i += 1
if not found:
	print(-1)


inp1 = input().split()
a = int(inp1[0])
b = int(inp1[1])
inp2 = input().split()
c = int(inp2[0])
d = int(inp2[1])

def pgcd(a, b):
	while a % b != 0:
		(a, b) = (b, a % b)
	return b
x = 0
y = 0
t = 0
pg = pgcd(a, c)
if abs(d - b) % pg == 0:
	while 1:
		if a * x + b == c * y + d:
			break
		if a * x + b < c * y + d:
			x += 1
		if a * x + b > c * y + d:
			y += 1
	t = a * x + b
	print(t)
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
l1 = []
l2 = []
for i in range(0, 1000):
	l1.append(b + i * a)
for j in range(0, 1000):
	if d + c * j in l1:
		print(d + c * j)
		break
else:
	print(-1)


import sys

def input():
	return sys.stdin.readline().rstrip('\r\n')

def List():
	return list(map(int, input().split()))

def Num():
	return int(input())

def solve():
	(a, b) = List()
	(c, d) = List()
	ok = False
	for i in range(500):
		for j in range(500):
			if b + a * i == d + c * j:
				return b + a * i
	return -1
print(solve())


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
r = []
t = []
for i in range(b, 50500, a):
	r.append(i)
for j in range(d, 50500, c):
	t.append(j)
if set(r) & set(t) == set():
	print('-1')
else:
	print(min(set(t).intersection(set(r))))


(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
l = []
for i in range(100):
	l.append(b + i * a)
flag = 0
for i in range(100):
	if d + i * c in l:
		flag = 1
		break
if flag == 1:
	print(d + i * c)
else:
	print(-1)


(b, a) = map(int, input().split(' '))
(d, c) = map(int, input().split(' '))
found = False
for i in range(0, 100):
	if a + i * b - c >= 0 and (a + i * b - c) % d == 0:
		print(a + i * b)
		found = True
		break
if not found:
	print('-1')


IL = lambda : list(map(int, input().split()))
I = lambda : int(input())
(a, b) = IL()
(c, d) = IL()
e = sorted(list(set(range(b, 20000, a)).intersection(set(range(d, 20000, c)))))
if len(e):
	print(e[0])
else:
	print(-1)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True

def main():
	(a, b) = mi()
	(c, d) = mi()
	g = math.gcd(a, c)
	if (d - b) % g != 0:
		print(-1)
		exit()
	while b != d:
		if b < d:
			b += a
		else:
			d += c
	print(b)
main()


def mp():
	return map(int, input().split())

def lt():
	return list(map(int, input().split()))

def pt(x):
	print(x)

def ip():
	return input()

def it():
	return int(input())

def sl(x):
	return [t for t in x]

def spl(x):
	return x.split()

def aj(liste, item):
	liste.append(item)

def bin(x):
	return '{0:b}'.format(x)

def listring(l):
	return ' '.join([str(x) for x in l])

def ptlist(l):
	print(' '.join([str(x) for x in l]))
(a, b) = mp()
(c, d) = mp()

def pgcd(x, y):
	if x > y:
		return pgcd(y, x)
	if y % x == 0:
		return x
	else:
		return pgcd(y % x, x)
if abs(b - d) % pgcd(a, c) != 0:
	pt(-1)
else:
	k = b
	s = d
	while k != s:
		if k > s:
			s += c
		else:
			k += a
	pt(k)


import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, gcd, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
(a, b) = invr()
(c, d) = invr()
g = b - d
if g % gcd(a, c) == 0:
	for y in range(101):
		for x in range(101):
			if x * c - y * a == b - d:
				print(b + y * a)
				sys.exit()
else:
	print(-1)


gcd = lambda a, b: a if b == 0 else gcd(b, a % b)
lcm = lambda a, b: a * b // (gcd(a, b) if a > b else gcd(b, a))

def f(l1, l2):
	(a, b) = l1
	(c, d) = l2
	s = max(b, d)
	e = s + lcm(a, c) + 1
	i = s
	while i < e:
		if (i - b) % a == 0 and (i - d) % c == 0:
			return i
		i += 1
	return -1
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
print(f(l1, l2))


inp = list(map(int, input().split()))
a = inp[0]
b = inp[1]
inp = list(map(int, input().split()))
c = inp[0]
d = inp[1]
x = d - b
y = c
k = 0
flag = 0
while k < a:
	if (x + k * y) % a == 0:
		flag = 1
		break
	else:
		k += 1
if flag == 0:
	print(-1)
else:
	k1 = (d - b + c * k) // a
	if k1 < 0:
		while k1 < 0:
			k1 += c
	print(b + a * k1)


(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
h = 0
for k in range(10000):
	t = b + k * a
	for i in range(10000):
		if t == d + i * c:
			print(d + i * c)
			h += 1
			break
	if h > 0:
		break
if h == 0:
	print('-1')


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
i = 0
j = 0

def GCD(a, b):
	if b == 0:
		return a
	return GCD(b, a % b)
if (d - b) % GCD(a, c):
	print(-1)
	exit()
while b + a * i != d + c * j:
	if b + a * i > d + c * j:
		j += 1
	else:
		i += 1
print(b + a * i)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
from collections import defaultdict
al = defaultdict(int)
import math
if b == d:
	print(b)
	exit()
elif b > d:
	rem = []
	b = b - d
	while 1:
		if b % c == 0:
			print(b + d)
			exit()
		else:
			al[b % c] += 1
			if al[b % c] > 1:
				print(-1)
				exit()
			else:
				b += a
else:
	rem = []
	d = d - b
	while 1:
		if d % a == 0:
			print(b + d)
			exit()
		else:
			al[d % a] += 1
			if al[d % a] > 1:
				print(-1)
				exit()
			else:
				d += c


(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))
for x in range(0, 101):
	f = False
	for y in range(0, 101):
		if a * x + b - (c * y + d) == 0:
			print(a * x + b)
			f = True
			break
	if f:
		break
if f == False:
	print(-1)


def is_even(num):
	return num % 2 == 0

def is_odd(num):
	return num % 2 == 1

def is_valid_scream(a, b, c, d, rick_scream_count):
	numerator = d + rick_scream_count * c - b
	if numerator < 0:
		return False
	return numerator % a == 0

def find_overlapping_screams(a, b, c, d):
	rick_is_always_odd = is_odd(b) and is_even(a)
	morty_is_always_odd = is_odd(d) and is_even(c)
	rick_is_always_even = is_even(b) and is_even(a)
	morty_is_always_even = is_even(d) and is_even(c)
	if rick_is_always_even and morty_is_always_odd or (rick_is_always_odd and morty_is_always_even):
		return -1
	rick_scream_count = 0
	count = 0
	max_enum = 100 ** 3
	while True:
		if is_valid_scream(a, b, c, d, rick_scream_count):
			break
		rick_scream_count += 1
		count += 1
		if count >= max_enum:
			return -1
	return d + rick_scream_count * c
(a, b) = list(map(int, input().strip().split(' ')))
(c, d) = list(map(int, input().strip().split(' ')))
overlapping_scream_time = find_overlapping_screams(a, b, c, d)
print(overlapping_scream_time)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
flag = False
for i in range(500):
	for j in range(500):
		if b + a * i == d + c * j:
			flag = True
			print(b + a * i)
		if flag:
			break
	if flag:
		break
if not flag:
	print(-1)


import math
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
while b != d:
	if min(b, d) > 20000:
		break
	if b > d:
		d += c
	else:
		b += a
print(b if b == d else -1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
v = set(range(b, 100 ** 2, a)) & set(range(d, 100 ** 2, c))
print(min(v) if v else -1)


import sys
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
for i in range(max(b, d), 100000):
	if (i - b) % a == 0 and (i - d) % c == 0:
		print(i)
		sys.exit()
print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
(x, y) = (0, 0)
s = 0
(bb, dd) = (b, d)
while 1:
	s = s + 1
	if s == 10000:
		print(-1)
		break
	if bb == dd:
		print(bb)
		break
	elif bb > dd:
		dd = d + c * x
		x = x + 1
	elif bb < dd:
		bb = b + a * y
		y = y + 1


from typing import Callable, Iterator
read: Callable[[], Iterator[int]] = lambda : map(int, input().split())
(b, a) = read()
(d, c) = read()
ans = -1
n = int(1000005)
for i in range(n):
	cur = a + i * b
	if (cur - c) % d == 0 and (cur - c) // d >= 0:
		ans = cur
		break
print(ans)


(a, b) = [int(c) for c in input().split(' ')]
(c, d) = [int(c) for c in input().split(' ')]
i = 0
max = pow(10, 5)
while b != d and i < max:
	while d < b:
		d += c
	while b < d:
		b += a
	i += 1
if b != d:
	print(-1)
else:
	print(d)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
rick = []
monty = []
for i in range(0, 101, 1):
	rick.append(b + i * a)
	monty.append(d + i * c)
c = 0
for ele1 in rick:
	for ele2 in monty:
		if ele1 == ele2:
			print(ele1)
			c = 1
			break
	if c == 1:
		break
if c == 0:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
res = -1
k = 0
while (k < a or (d + (k - 1) * c - b) // a < 0) and res == -1:
	if (d + k * c - b) % a == 0 and (d + k * c - b) // a >= 0:
		res = d + k * c
	k += 1
print(res)


g = lambda x, y: g(y, x % y) if y else x
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
e = g(a, c)
if (b - d) % e:
	print(-1)
else:
	while b != d:
		if b < d:
			b += a
		else:
			d += c
	print(b)


import sys
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
times1 = [b + a * i for i in range(100)]
for time2 in [d + c * i for i in range(100)]:
	if time2 in times1:
		print(time2)
		sys.exit()
print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
f = []
s = []
ans = -1
for i in range(10000):
	ff = b + i * a
	f.append(ff)
for i in range(10000):
	ss = d + i * c
	if ss in f:
		ans = ss
		break
	s.append(ss)
print(ans)


import sys
(mod, MOD) = (1000000007, 998244353)

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()

def extended_euclid(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(gcd, x, y) = extended_euclid(b % a, a)
		return (gcd, y - b // a * x, x)
(a, b) = get_ints()
(c, d) = get_ints()
(gcd, x, y) = extended_euclid(a, -c)
if (d - b) % gcd != 0:
	print(-1)
	exit()
while b != d:
	if b < d:
		b += a
	else:
		d += c
print(b)


def scream_times(a, b, c, d):
	n = 1000
	for i in range(n):
		for j in range(n):
			if b + i * a == d + c * j:
				return b + i * a
	return -1
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
print(scream_times(a, b, c, d))


from fractions import gcd

def ri():
	return map(int, input().split())
(a, b) = ri()
(c, d) = ri()
if c > a:
	(a, c) = (c, a)
	(b, d) = (d, b)
for i in range(10 ** 5):
	if b - d + i * a >= 0 and (b - d + i * a) % c == 0:
		print(b + i * a)
		exit()
else:
	print(-1)


from math import *
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
x = []
y = []
for i in range(0, 101):
	x.append(b + a * i)
	y.append(d + c * i)
for i in range(0, 101):
	for j in range(0, 101):
		if x[i] - y[j] == 0:
			print(x[i])
			exit()
print(-1)


from sys import stdin, stdout
(a, b) = map(int, stdin.readline().split())
(c, d) = map(int, stdin.readline().split())
(c1, d1) = max((c, d), (a, b), key=lambda x: x[1])
(a1, b1) = min((c, d), (a, b), key=lambda x: x[1])
for y in range(10 ** 6):
	if (d1 + c1 * y - b1) / a1 == int((d1 + c1 * y - b1) / a1) >= 0 and d1 + y * c1 == b1 + a1 * int((d1 + c1 * y - b1) / a1):
		stdout.write(str(d1 + c1 * y))
		break
else:
	stdout.write('-1')


import math

def bezout(m, n, t):
	x = 0
	while (t - m * x) % n != 0:
		x += 1
	return (x, int(t - m * x) / n)
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
if (d - b) % math.gcd(a, c) != 0:
	print(-1)
else:
	(t1, t2) = bezout(a, c, d - b)
	while t2 > 0:
		t1 += int(c / math.gcd(a, c))
		t2 -= int(a / math.gcd(a, c))
	print(b + a * t1)


import sys
input = sys.stdin.readline
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
found = False
for i in range(int(1000000.0)):
	j = (a * i + b - d) / c
	if j % 1 == 0 and j >= 0:
		print(a * i + b)
		found = True
		break
if not found:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
cnt = 0
founded = False
j = 0
while cnt <= a:
	i = (d - b + c * j) // a
	if i >= 0:
		cnt += 1
		if (d - b + c * j) % a == 0:
			founded = True
			break
	j += 1
if founded:
	print(b + a * i)
else:
	print(-1)


from math import gcd

def main():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	n = a * c // gcd(a, c) + b + d
	l = [*[0] * n, 2]
	for i in range(b, n, a):
		l[i] = 1
	for i in range(d, n, c):
		l[i] += 1
	i = l.index(2)
	print(i if i < n else -1)
main()


(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
l1 = []
l2 = []
for i in range(1, 101):
	f1 = b + (i - 1) * a
	f2 = d + (i - 1) * c
	l1.append(f1)
	l2.append(f2)
an = True
for i in range(len(l1)):
	if l1[i] in l2:
		print(l1[i])
		an = False
		break
if an == True:
	print(-1)


n = input().split()
a = int(n[0])
b = int(n[1])
m = input().split()
c = int(m[0])
d = int(m[1])
for i in range(100):
	for j in range(100):
		if a * i + b == c * j + d:
			print(b + a * i)
			exit()
print('-1')


(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
current = min(b, d)
if current == b:
	inc = a
	sub = d
	mod = c
else:
	inc = c
	sub = b
	mod = a
done = False
for i in range(100000):
	if current >= sub and (current - sub) % mod == 0:
		firstanswer = current
		done = True
		break
	else:
		current += inc
if not done:
	firstanswer = float('inf')
current = max(b, d)
if current == b:
	inc = a
	sub = d
	mod = c
else:
	inc = c
	sub = b
	mod = a
done = False
for i in range(100000):
	if current >= sub and (current - sub) % mod == 0:
		secondanswer = current
		done = True
		break
	else:
		current += inc
if not done:
	secondanswer = float('inf')
if firstanswer == float('inf') and secondanswer == float('inf'):
	print(-1)
else:
	print(min(firstanswer, secondanswer))


import sys
import math
import bisect

def solve(a, b, c, d):
	A = []
	B = []
	for i in range(200):
		A.append(b + a * i)
	for i in range(200):
		B.append(d + c * i)
	s = set(A) & set(B)
	if len(s) == 0:
		return -1
	return min(s)

def main():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	ans = solve(a, b, c, d)
	print(ans)
main()


ans = []
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
l = [b, b + a]
ll = [d, d + c]
for i in range(100):
	l += [b + i * a]
for i in range(100):
	ll += [d + i * c]
for i in l:
	if i in ll:
		ans += [i]
if len(ans) == 0:
	print(-1)
else:
	print(ans[0])


r = lambda : map(int, input().split())
(a, b) = r()
(c, d) = r()
common = set(range(b, 100 ** 2, a)) & set(range(d, 100 ** 2, c))
print(min(common) if common else -1)


def gcd(a, b):
	return a if b == 0 else gcd(b, a % b)
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
g = gcd(a, c)
dif = abs(b - d)
if dif % g == 0:
	for t in range(max(b, d), 1000000):
		if (t - b) % a == 0 and (t - d) % c == 0:
			print(t)
			break
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
ptr1 = b
arr1 = [0] * c
while ptr1 < d:
	ptr1 += a
flag = 0
if ptr1 % c == d % c:
	flag = 1
while max(arr1) != 2 and flag == 0:
	ptr1 += a
	if ptr1 % c == d % c:
		flag = 1
		break
	arr1[ptr1 % c] += 1
if flag == 0:
	print(-1)
else:
	print(ptr1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
f = set()
s = set()
ans = -1
i = 0
while ans == -1 and i < 10000:
	ff = b + i * a
	ss = d + i * c
	f.add(ff)
	s.add(ss)
	if f & s:
		ans = min(f & s)
	i += 1
print(ans)


(x, y) = map(int, input().split())
(z, t) = map(int, input().split())
tmp = 0

def timluot(a, b, c, d):
	i = 0
	tm = 0
	while i <= 200:
		j = 0
		tm = b + a * i
		while True:
			tmp = d + c * j
			if tmp == tm:
				return tmp
			elif tmp > tm:
				break
			j += 1
		i += 1
	return 0
tmp = timluot(x, y, z, t)
if tmp != 0:
	print(tmp)
else:
	print(-1)


line = input()
(a, b) = line.split()
(a, b) = (int(a), int(b))
line = input()
(c, d) = line.split()
(c, d) = (int(c), int(d))
x = b
x_count = 1
y = d
y_count = 1
while y != x and y <= 10100 and (x <= 10100):
	if y > x:
		x = b + x_count * a
		x_count = x_count + 1
	else:
		y = d + y_count * c
		y_count = y_count + 1
if y == x:
	print(y)
else:
	print(-1)


(a, b) = map(int, input().split())
s = 0
(c, d) = map(int, input().split())
flag = 0
while d != b:
	if s == 100000:
		print(-1)
		b = d
		flag = 1
	if d < b:
		d += max(c * ((b - d) // c), c)
	else:
		b += max(a * ((d - b) // a), a)
	s += 1
if flag == 0:
	print(b)


import math
(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
k = math.gcd(a, c)
if abs(b - d) % k == 0:
	for i in range(101):
		for j in range(101):
			if b + a * i == d + c * j:
				print(b + a * i)
				exit()
else:
	print(-1)


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

def isEvenOrOdd(a, b):
	if a % 2 == 0 and b % 2 == 0:
		return 'Even'
	elif a == 2 * b:
		return 'Odd'
	else:
		return 'None'
if isEvenOrOdd(a, b) == 'Even' and isEvenOrOdd(c, d) == 'Odd' or (isEvenOrOdd(a, b) == 'Odd' and isEvenOrOdd(c, d) == 'Even'):
	print(-1)
else:
	times = -1
	while n <= 100:
		if (a * n - d + b) % c == 0 and (a * n - d + b) % c >= 0 and (b + a * n >= b) and (d + (a * n - d + b) // c * c >= d):
			times = b + a * n
			break
		else:
			n += 1
	print(times)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
s = {b - d}
while b != d:
	if b < d:
		b += a
	else:
		d += c
	bd = b - d
	if bd in s:
		break
	s |= {bd}
if d == b:
	print(b)
else:
	print(-1)


(a, b) = [int(f) for f in input().split()]
(c, d) = [int(f) for f in input().split()]
t = True

def findy(x):
	return (b - d + a * x) / c
for n in range(1000):
	if findy(n) == int(findy(n)) and findy(n) >= 0:
		print(b + a * n)
		t = False
		break
if t:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
res = -1
m = max(a, b, c, d)
for i in range(0, m + 1):
	j = (a * i + b - d) // c
	jd = (a * i + b - d) % c
	if jd == 0 and a * i + b >= max(b, d):
		res = a * i + b
		break
print(res)


(a, b) = [int(s) for s in input().split()]
(c, d) = [int(s) for s in input().split()]
s = []
for x in range(101):
	for y in range(101):
		l = b + x * a
		m = d + y * c
		if l == m:
			s.append(l)
if len(s) == 0:
	print(-1)
else:
	print(s[0])


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
MAX = 10 ** 5
for pos in range(d, MAX, c):
	if pos in range(b, MAX, a):
		print(pos)
		break
else:
	print(-1)


(x, y) = map(int, input().split())
(p, q) = map(int, input().split())
for i in range(1, 101):
	for j in range(1, 101):
		if y + (i - 1) * x == q + (j - 1) * p:
			print(y + (i - 1) * x)
			exit()
print(-1)


import math
(a, b) = list(map(int, input().split()))
(c, d) = list(map(int, input().split()))
f = abs(d - b)
if f % math.gcd(a, c) == 0:
	while b != d:
		if b > d:
			d = d + c
		else:
			b = b + a
	print(b)
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
while b != d and max(b, d) < pow(10, 4) + 1:
	if b < d:
		b += a
	else:
		d += c
if b == d:
	print(b)
else:
	print(-1)


from fractions import gcd
(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
g = gcd(a, c)
if abs(b - d) % g != 0:
	print(-1)
else:
	s1 = set([b])
	s2 = set([d])
	while True:
		if b in s2:
			print(b)
			break
		if d in s1:
			print(d)
			break
		b += a
		d += c
		s1.add(b)
		s2.add(d)


def sum_(a_, d_):
	return a_ + n * d_
(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))
lim = a * c + b + d
n = 0
sli = [sum_(b, a)]
while sli[n] <= lim:
	n += 1
	sli.append(sum_(b, a))
n = 0
slj = [sum_(d, c)]
while slj[n] <= lim:
	n += 1
	slj.append(sum_(d, c))
r = -1
for i in sli:
	for j in slj:
		if i == j:
			r = j
			break
	if i == r:
		break
print(r)


(a, b) = map(int, input().split())
(r, l) = map(int, input().split())
for i in range(0, 101):
	for j in range(101):
		if a * i + b == l + r * j:
			print(a * i + b)
			exit()
print(-1)


(b, a) = map(int, input().split())
(d, c) = map(int, input().split())
ans = -1
for i in range(100100):
	if i >= a and i >= c and (i % b == a % b) and (i % d == c % d) and (ans == -1):
		ans = i
print(ans)


(a, b) = input().split()
(c, d) = input().split()
(a, b, c, d) = (int(a), int(b), int(c), int(d))
if d > b:
	(a, b, c, d) = (c, d, a, b)
d = d % c
for i in range(c):
	res = b + a * i
	if res % c == d:
		print(res)
		break
else:
	print('-1')


(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
vmax = max(a, b, c, d)
for i in range(vmax + 1):
	temp = a * i + b
	if (temp - d) % c == float(0) and temp - d >= 0:
		print(temp)
		break
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
h = 0
k = 0
while h <= 100:
	if (d + h * c - b) % a == 0 and d + h * c - b >= 0:
		k = 1
		break
	h += 1
if k == 0:
	print('-1')
else:
	print(d + h * c)


(a, b) = [int(i) for i in input().split()]
(c, d) = [int(i) for i in input().split()]
z = max(b, d)
while True:
	if z % a == b % a and z % c == d % c:
		print(z)
		break
	elif z > 100 * 100:
		print(-1)
		break
	z += 1


from math import floor
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
found = False
for i in range(105):
	B = b + i * a
	j = (B - d) / c
	if j >= 0 and floor(j) == j:
		print(b + i * a)
		found = True
		break
if not found:
	print(-1)


def rickAndMorty():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	rickStep = 0
	mortyStep = 0
	rick = b
	morty = d
	loop = 0
	while True:
		loop += 1
		if loop == 10000:
			print(-1)
			return
		if rick == morty:
			print(rick)
			return
		if rick > morty:
			morty = d + mortyStep * c
			mortyStep += 1
		elif rick < morty:
			rick = b + rickStep * a
			rickStep += 1
rickAndMorty()


def e_gcd(a, b):
	s = pt = 0
	ps = t = 1
	r = b
	pr = a
	while r:
		q = pr // r
		(pr, r) = (r, pr - q * r)
		(ps, s) = (s, ps - q * s)
		(pt, t) = (t, pt - q * t)
	return (pr, (ps, pt))
(a, b) = [int(x) for x in input().split()]
(c, d) = [int(x) for x in input().split()]
(g, x) = e_gcd(a, c)
num = d - b
if num % g:
	print(-1)
else:
	(u, v) = x
	xp = num * u // g
	hs = c // g
	if not u:
		xp += num // g
	while xp > 0:
		xp -= hs
	while xp < 0:
		xp += hs
	ans = xp * a + b
	if ans < d:
		(o, p) = e_gcd(a, c)
		while ans < d:
			ans += a * c // o
	print(ans)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
e = []
g = []
l = 0
for i in range(101):
	h = b + i * a
	e.append(h)
	k = d + i * c
	g.append(k)
for i in range(len(e)):
	if e[i] in g:
		l = 1
		s = e[i]
		break
if l == 1:
	print(e[i])
else:
	print(-1)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
z = -1
for i in range(1000):
	for j in range(1000):
		if b + a * i == d + c * j:
			z = b + a * i
			break
	if z > -1:
		break
print(z)


import sys
import re
input = lambda : sys.stdin.readline()
import functools

@functools.lru_cache(maxsize=10000)
def Main():
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	Rick = 0
	Morty = 0
	A = []
	B = []
	A.append(b)
	B.append(d)
	for i in range(1, 501):
		A.append(b + a * i)
		B.append(d + c * i)
	flag = 0
	for i in range(501):
		for j in range(501):
			if A[i] == B[j]:
				print(A[i])
				flag = 1
				break
		if flag == 1:
			break
	if flag == 0:
		print(-1)
Main()


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

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
if abs(d - b) % gcd(a, c) != 0:
	print('-1')
	exit(0)
res = 1000000
for k in range(1000):
	for l in range(1000):
		if k * a + b == l * c + d:
			res = min(res, k * a + b)
print(res)


(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))
x = [b + i * a for i in range(100)]
y = [d + i * c for i in range(100)]
res = -1
for t in x:
	if t in y:
		res = t
		break
print(res)


(a, b) = input().strip().split(' ')
(c, d) = input().strip().split(' ')
(a, b, c, d) = (int(a), int(b), int(c), int(d))
for i in range(1000):
	for j in range(1000):
		l1 = b + i * a
		l2 = d + j * c
		if l1 == l2:
			print(l1)
			exit()
print(-1)


(b, a) = map(int, input().split())
(d, c) = map(int, input().split())
ans = 10 ** 10
for i in range(111):
	for j in range(111):
		if a + i * b == c + j * d:
			ans = min(ans, a + i * b)
if ans == 10 ** 10:
	ans = -1
print(ans)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
x = [b + a * i for i in range(3 * 10 ** 3)]
y = [d + c * i for i in range(3 * 10 ** 3)]
for i in x:
	if i in y:
		print(i)
		break
else:
	print(-1)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def main():
	(a, b) = LI()
	(c, d) = LI()
	t = 0
	while b != d:
		t += 1
		if t > 100000:
			b = -1
			break
		if b < d:
			b += a
		else:
			d += c
	return b
print(main())


(a, b) = input().split()
(a, b) = (int(a), int(b))
(c, d) = input().split()
(c, d) = (int(c), int(d))
i = 0
for i in range(1000001):
	if (b - d) % c == 0 and (b - d) / c >= 0:
		print(b)
		break
	b += a
	if i == 1000000:
		print(-1)
		break


from os import path
import sys, time
from math import ceil, floor, gcd, log, log2, factorial, sqrt
from collections import defaultdict, Counter, OrderedDict, deque
from itertools import combinations, accumulate
from bisect import *
from functools import reduce
from operator import mul
star = lambda x: print(' '.join(map(str, x)))
grid = lambda r: [lint() for i in range(r)]
INF = float('inf')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
import sys
from sys import stdin, stdout
from collections import *
from math import gcd, floor, ceil

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def inlt():
	return list(map(int, stdin.readline().split()))

def invr():
	return map(int, stdin.readline().split())

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def gcdExtended(a, b):
	if a == 0:
		return (b, 0, 1)
	(gcd, x1, y1) = gcdExtended(b % a, a)
	x = y1 - b // a * x1
	y = x1
	return (gcd, x, y)

def LDA(a, b, c):
	(g, x0, y0) = gcdExtended(a, b)
	x = x0 * (c // g)
	y = y0 * (c // g)
	return (x, y)

def solve():
	(d1, a1) = invr()
	(d2, a2) = invr()
	s = set(range(a1, 10000, d1)) & set(range(a2, 10000, d2))
	print(min(s) if s else -1)
t = 1
for _ in range(t):
	solve()


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
checker = False
for i in range(100):
	for k in range(100):
		if b + i * a == d + k * c:
			print(b + i * a)
			checker = True
		if checker:
			break
	if checker:
		break
if not checker:
	print('-1')


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
if b < d:
	(a, b, c, d) = (c, d, a, b)
s = set()
while True:
	t = (b - d) % c
	if t in s:
		print('-1')
		break
	if t == 0:
		print(b)
		break
	b += a
	s.add(t)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
ans = -1
for i in range(1, 101):
	x = b + (i - 1) * a
	for j in range(1, 101):
		y = d + (j - 1) * c
		if y == x:
			ans = y
			print(ans)
			quit(0)
		if y > x:
			break
print(ans)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
res = -1
for i in range(100):
	for j in range(100):
		if b + a * i == d + c * j:
			res = b + a * i
			break
	else:
		continue
	break
print(res)


(a, b) = (int(x) for x in input().split())
(c, d) = (int(x) for x in input().split())
x = b
mod = []
cont = True
while cont:
	y = x % c
	if x < d:
		x += a
		continue
	if y in mod:
		cont = False
		print(-1)
	elif (y - d) % c == 0:
		cont = False
		print(x)
	else:
		mod.append(y)
	x += a


def form(a, b, c, d):
	n = 1000
	for i in range(0, n + 1):
		for j in range(0, n + 1):
			if b + a * i == d + c * j:
				return b + a * i
				break
	return -1
(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
print(form(a, b, c, d))


import sys
input = sys.stdin.readline
(a, b) = map(int, input().strip().split())
(c, d) = map(int, input().strip().split())
arr1 = [b + a * i for i in range(100)]
arr2 = set([d + c * i for i in range(100)])
for ele in arr1:
	if ele in arr2:
		exit(print(ele))
print(-1)


(a, b) = [int(i) for i in input().split()]
(c, d) = [int(i) for i in input().split()]
res = [0 for i in range(10000000)]
for i in range(10000):
	res[int(b + a * i)] = 1
for i in range(10000):
	if res[d + c * i] == 1:
		print(d + c * i)
		exit()
print(-1)


read = lambda : map(int, input().split())
(b, a) = read()
(d, c) = read()
ans = -1
N = int(1100000.0)
for i in range(N):
	cur = a + i * b
	if (cur - c) % d == 0 and (cur - c) // d >= 0:
		ans = cur
		break
print(ans)


import math
(a, b) = map(int, input().strip().split(' '))
(c, d) = map(int, input().strip().split(' '))
f = 0
k = 0
if a % 2 == 0 and b % 2 == 0 and (c % 2 == 0) and (d % 2 != 0):
	print(-1)
elif c % 2 == 0 and d % 2 == 0 and (a % 2 == 0) and (b % 2 != 0):
	print(-1)
else:
	for i in range(0, 101):
		for j in range(0, 101):
			k = i * a - j * c
			if k == d - b:
				f = 1
				print(b + i * a)
				break
		if f == 1:
			break
	if f == 0:
		print(-1)


def ext_gcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, x, y) = ext_gcd(b % a, a)
		return (g, y - b // a * x, x)
(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))
min = -1
for i in range(120):
	if (b + i * a) % c == d % c and b + i * a >= d:
		if min == -1 or min > b + i * a:
			min = b + i * a
	if (d + i * c) % a == b % a and d + i * c >= b:
		if min == -1 or min > d + i * c:
			min = d + i * c
print(min)


(a, b) = map(int, input().split())
(c, d) = map(int, input().split())
x = [b]
y = [d]
for i in range(1000):
	if x[len(x) - 1] in y:
		print(x[len(x) - 1])
		break
	elif y[len(y) - 1] in x:
		print(y[len(y) - 1])
		break
	x.append(x[len(x) - 1] + a)
	y.append(y[len(y) - 1] + c)
else:
	print(-1)


arr = input().split(' ')
a = int(arr[0])
b = int(arr[1])
arr = input().split(' ')
c = int(arr[0])
d = int(arr[1])
k = set(range(b, 10 ** 4, a))
l = set(range(d, 10 ** 4, c))
j = k & l
print(min(j) if j else -1)