import sys input=lambda: sys.stdin.readline().rstrip() x1,y1=map(int,input().split()) x2,y2=map(int,input().split()) x3,y3=map(int,input().split()) ans=x1 ct=0 chk=False while ct<=y2: if ans%y2==x2: chk=True break else: ans+=y1 ct+=1 def gcd(a,b): while b: a,b=b,a%b return a if chk==False: print(-1) else: chk=False ct=0 d=y1*y2//gcd(y1,y2) while ct<=y3: if ans%y3==x3: chk=True break else: ans+=d ct+=1 if chk: print(ans) else: print(-1)