import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } struct ModInt(uint M_) { import std.conv : to; alias M = M_; uint x; this(ModInt a) { x = a.x; } this(uint x_) { x = x_ % M; } this(ulong x_) { x = x_ % M; } this(int x_) { x = ((x_ %= cast(int)(M)) < 0) ? (x_ + cast(int)(M)) : x_; } this(long x_) { x = cast(uint)(((x_ %= cast(long)(M)) < 0) ? (x_ + cast(long)(M)) : x_); } ref ModInt opAssign(T)(inout(T) a) if (is(T == uint) || is(T == ulong) || is(T == int) || is(T == long)) { return this = ModInt(a); } ref ModInt opOpAssign(string op, T)(T a) { static if (is(T == ModInt)) { static if (op == "+") { x = ((x += a.x) >= M) ? (x - M) : x; } else static if (op == "-") { x = ((x -= a.x) >= M) ? (x + M) : x; } else static if (op == "*") { x = cast(uint)((cast(ulong)(x) * a.x) % M); } else static if (op == "/") { this *= a.inv(); } else static assert(false); return this; } else static if (op == "^^") { if (a < 0) return this = inv()^^(-a); ModInt b = this, c = 1U; for (long e = a; e; e >>= 1) { if (e & 1) c *= b; b *= b; } return this = c; } else { return mixin("this " ~ op ~ "= ModInt(a)"); } } ModInt inv() const { uint a = M, b = x; int y = 0, z = 1; for (; b; ) { const q = a / b; const c = a - q * b; a = b; b = c; const w = y - cast(int)(q) * z; y = z; z = w; } assert(a == 1); return ModInt(y); } ModInt opUnary(string op)() const { static if (op == "+") { return this; } else static if (op == "-") { ModInt a; a.x = x ? (M - x) : 0U; return a; } else static assert(false); } ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); } ModInt opBinaryRight(string op, T)(T a) const { return mixin("ModInt(a) " ~ op ~ "= this"); } bool opCast(T: bool)() const { return (x != 0U); } string toString() const { return x.to!string; } } enum MO = 998244353; alias Mint = ModInt!MO; // !!!old library!!! Int mod(Int)(Int a, Int m) { if ((a %= m) < 0) a += m; return a; } Int gcd(Int)(Int a, Int b) { return (b != 0) ? gcd(b, a % b) : a; } Int lcm(Int)(Int a, Int b) { return a / gcd(a, b) * b; } Int gojo(Int)(Int a, Int b, out Int x, out Int y) { if (b != 0) { Int g = gojo(b, a % b, y, x); y -= (a / b) * x; return g; } x = 1; y = 0; return a; } Int modInv(Int)(Int a, Int m) { Int x, y; gojo(a, m, x, y); return mod(x, m); } long modLogP(long a, long b, long m) { a = mod(a, m); b = mod(b, m); if (m == 1) return 0; long k, ak = 1; Tuple!(long,long)[] as; for (k = 0; k * k < m; ++k) { as ~= tuple(ak, k); ak = (ak * a) % m; } as.sort; ak = modInv(ak, m); for (long i = 0; i < k; ++i) { int pos = as.lowerBound(tuple(b, 0L)); if (pos < as.length && as[pos][0] == b) return i * k + as[pos][1]; b = (b * ak) % m; } return -1; } long modLog(long a, long b, long m) { a = mod(a, m); b = mod(b, m); long f, g, r = 1 % m; for (f = 0; (g = gcd(a, m)) > 1; ++f) { if (b % g != 0) return (r == b) ? f : -1; b /= g; m /= g; r = (r * (a / g)) % m; } long res = modLogP(a, b * modInv(r, m) % m, m); return (res != -1) ? (f + res) : -1; } enum L = 10L^^5; void main() { try { for (; ; ) { const Mint A = readLong(); const Mint B = readLong(); const Mint P = readLong(); const Mint Q = readLong(); long ans = -1; if (B) { /* [ A -B ] [ 1 ] */ Mint[2][2] c; c[0][0] = c[1][1] = 1; long[ulong] app; foreach (y; 0 .. L) { const p = c[0][0] * P + c[0][1] * Q; const q = c[1][0] * P + c[1][1] * Q; const key = cast(ulong)(p.x) << 30 | q.x; app[key] = y; debug { if (L - y <= 10) { writeln(y, " ", p, " ", q, " ", key); stdout.flush; } } Mint tmp; tmp = A * c[0][0] - B * c[1][0]; c[1][0] = c[0][0]; c[0][0] = tmp; tmp = A * c[0][1] - B * c[1][1]; c[1][1] = c[0][1]; c[0][1] = tmp; } debug { writeln("c = ", c); stdout.flush; } { Mint p = A, q = 2; for (long x = 1; ; ++x) { const pp = c[0][0] * p + c[0][1] * q; const qq = c[1][0] * p + c[1][1] * q; p = pp; q = qq; const key = cast(ulong)(p.x) << 30 | q.x; debug { if (x <= 10) { writeln("! ", p, " ", q, " ", key); stdout.flush; } } if (key in app) { // f^(L x) (A, 2) = f^y (P, Q) const y = app[key]; ans = L * x - y + 1; break; } } } } else if (A) { /* X = 0, Y = A P = A^N, Q = A^(N-1) */ assert(P == A * Q); ans = modLogP(A.x, P.x, MO); for (; ans < 2; ans += MO - 1) {} } else { assert(!P); assert(!Q); ans = 2; } writeln(ans); } } catch (EOFException e) { } }