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.

Difficulty: interview

Starter code:

Solutions:
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)

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


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

a, b = list(map(int, input().split()))
c, d = list(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)
        return
    cr += c
print(-1)


from sys import stdin, stdout


a, b = list(map(int, stdin.readline().rstrip().split()))
c, d = list(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)


import math

a, b = list(map(int, input().split()))
c, d = list(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)


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)


import sys
a, b =  list(map(int, input().split()))
c, d  = list(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)
        return
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


from fractions import gcd

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

#ax+b=cy+d

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)





import sys
a, b = list(map(int, input().split()))
c, d = list(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)
                return
print(-1)


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)

a,b=list(map(int,input().split()))
s=0
c,d=list(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)


from fractions import gcd

a, b = list(map(int, input().split()))
c, d = list(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())
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 = 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)


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=[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())
if b > d:
    b, d = d, b
    a, c = c, a
d -= b
for i in range(1000000):
    if (c * i + d) % a == 0:
        print(i * c + d + b)
        break
else:
    print(-1)

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

"""
def nok(a, b):
    r = a
    while r % b != 0:
        r += a
    return r

def get_answer():
    if b == d:
        return 0

    if c == a:
        return -1

    if (b - d) * (c - a) < 0:
        return -1

    k = nok(abs(b - d), abs(c - a))
    return b + k * a
"""

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




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

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

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)


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)