# -*- coding: utf-8 -*- #yuki_186 import sys def gcd(a, b): while b: a, b = b, a%b return a def lcm(a,b): return a/gcd(a,b)*b x1,y1=map(int,raw_input().split()) x2,y2=map(int,raw_input().split()) x3,y3=map(int,raw_input().split()) for i in xrange(y2): a0=y1*i+x1 if a0==0: continue if (a0-x2)%y2==0: for j in xrange(y3): res=a0+j*lcm(y1,y2) if (res-x3)%y3==0: print res sys.exit() print -1