def gcd(a, b): if b == 0: return a else: return gcd(b, a%b) def extgcd(a, b): #拡張ユークリッド互除法 #ax + by = gcd(a, b)となる(x, y)を求めます if b == 0: return [1, 0] q = a//b r = a%b s, t = extgcd(b, r) y = s - q*t return [t, y] def CRT(b, m): r = 0 M = 1 for i in range(len(b)): d = gcd(M, m[i]) p, q = extgcd(M, m[i]) if (b[i] - r)%d != 0: print(-1) exit(0) t = (b[i] - r)//d * p % (m[i]//d) r += M * t M *= m[i]//d return r%M x = [] y = [] for i in range(3): X, Y = map(int, input().split()) x.append(X) y.append(Y) print(CRT(x, y))