MOD = 998244353 a,b,p,q = map(int,input().split()) T = 2*10**5 memo = {} f = lambda x,y: (y,(a*y-b*x)%MOD) B = (q,p) for i in range(T): memo[B] = i B = f(*B) v,w = (1,0),(0,1) for _ in range(T): v,w = f(*v),f(*w) c1,c2 = v c3,c4 = w g = lambda x,y: ((c1*x+c3*y)%MOD,(c2*x+c4*y)%MOD) s = f(2,a) for j in range(1,T+1): s = g(*s) if s in memo: i = memo[s] print(j*T-i+2) exit()