def exgcd(a, b): if b==0: return 1,0,a r = exgcd(b, a%b) return r[1],r[0]-a//b*r[1],r[2] def crt(a0, m0, a1, m1): g = exgcd(m0,m1)[2] if a0%g!=a1%g: return -1,-1 if g > 1: m0//=g m1//=g while True: h = exgcd(m0,g)[2] if h==1: break m0*=h g//=h m1 *= g a0 %= m0 a1 %= m1 r = exgcd(m0, m1) p,q,g=r[0],r[1],r[2] x = a0*q*m1+a1*p*m0 m=m0*m1 x%=m if x<0: x+=m return x,m X=[] Y=[] for i in range(3): x, y = map(int, input().split()) X.append(x) Y.append(y) x, y = X[0], Y[0] for i in range(1, 3): x, y = crt(x, y, X[i], Y[i]) if x==-1: break if x==0: print(y) else: print(x)