#pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") #include using namespace std; const long long MOD = 998244353; int main(){ long long A, B, P, Q; cin >> A >> B >> P >> Q; B = MOD - B; long long x = (A * A + B * 2) % MOD, y = A, N = 2; while (true){ if (N == 998244353) break; if (x == P && y == Q){ cout << N << endl; break; } y = (x * A + y * B) % MOD; swap(x, y); N++; } }