mod = 998244353 def mul(l, r): la, lb, lc, ld = l ra, rb, rc, rd = r return (la*ra+lb*rc) % mod, (la*rb+lb*rd) % mod, (lc*ra+ld*rc) % mod, (lc*rb+ld*rd) % mod def inv(x): a, b, c, d = x det = (a * d - b * c) % mod det_inv = pow(det, mod-2, mod) y = d, -b, -c, a return tuple(i * det_inv % mod for i in y) def mat_pow(a, x): b = 1, 0, 0, 1 while x: if x % 2 == 1: b = mul(b, a) a = mul(a, a) x //= 2 return b def bsgs(mat, a, b): k = 10 ** 5 + 100 mat_k = mat_pow(mat, k) giant = {} x = a for i in range(k): giant[x] = i x = mul(mat_k, x) mat_inv = inv(mat) x = b for i in range(k): if x in giant: return giant[x] * k + i x = mul(mat_inv, x) return None a, b, p, q = map(int, input().split()) mat = a, -b, 1, 0 start = a ** 2 - 2 * b, 0, a, 0 finish = p, 0, q, 0 n = bsgs(mat, start, finish) + 2 print(n)