#include using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define repe(i, l, r) for (int i = (l); i < (r); i++) #define reper(i, l, r) for (int i = (r) - 1; i >= (l); i--) #define repi(i, l, r) for (int i = (l); i <= (r); i++) #define repir(i, l, r) for (int i = (r); i >= (l); i--) #define range(a) a.begin(), a.end() void initio() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } ll MOD; class mint { ll n; public: mint(ll n_ = 0) : n(n_) {} explicit operator ll() { return n; } friend mint operator-(mint a) { return -a.n + MOD * (a.n != 0); } friend mint operator+(mint a, mint b) { ll x = a.n + b.n; return x - (x >= MOD) * MOD; } friend mint operator-(mint a, mint b) { ll x = a.n - b.n; return x + (x < 0) * MOD; } friend mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; } friend mint &operator+=(mint &a, mint b) { return a = a + b; } friend mint &operator-=(mint &a, mint b) { return a = a - b; } friend mint &operator*=(mint &a, mint b) { return a = a * b; } friend bool operator==(mint a, mint b) { return a.n == b.n; } friend bool operator<(mint a, mint b) { return a.n < b.n; } friend bool operator!=(mint a, mint b) { return a.n != b.n; } friend istream &operator>>(istream &i, mint &a) { return i >> a.n; } friend ostream &operator<<(ostream &o, mint a) { return o << a.n; } }; mint modpow(mint a, ll b) { mint res = 1; while (b > 0) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; } mint modinv(mint n) { ll a = (ll)n, b = MOD; ll s = 1, t = 0; while (b != 0) { int q = a / b; a -= q * b; s -= q * t; swap(a, b); swap(s, t); } return s >= 0 ? s : s + MOD; } using mat = vector; mat E = {1, 0, 0, 1}; mint det(mat A) { return A[0] * A[3] - A[1] * A[2]; } mat inv(mat A) { mint d = modinv(det(A)); mat res(4); res[0] = A[3] * d; res[1] = -A[1] * d; res[2] = -A[2] * d; res[3] = A[0] * d; return res; } mat mul(mat A, mat B) { mat res(4); res[0] = A[0] * B[0] + A[1] * B[2]; res[1] = A[0] * B[1] + A[1] * B[3]; res[2] = A[2] * B[0] + A[3] * B[2]; res[3] = A[2] * B[1] + A[3] * B[3]; return res; } mat matpow(mat A, ll B) { mat res = E; while (B > 0) { if (B & 1) res = mul(res, A); A = mul(A, A); B >>= 1; } return res; } ll bsgs(mint a, mint b) { constexpr ll S = 70000; map mp; mint R = 1; for (int i = 0; i < S; i++) { if (!mp.count(R)) { mp[R] = i; } R *= a; } // A^{50000i + j} = B // A^j = B^A{-50000i} R = modinv(R); for (int i = 0; i < S; i++) { if (mp.count(b)) { return S*i + mp[b]; } b *= R; } return -1; } ll bsgs_except_zero(mint a, mint b) { constexpr ll S = 70000; map mp; mint R = 1; for (int i = 0; i < S; i++) { if (!mp.count(R * a)) { mp[R * a] = i + 1; } R *= a; } // A^{50000i + j} = B // A^j = B^A{-50000i} R = modinv(R); for (int i = 0; i < S; i++) { if (mp.count(b)) { return S*i + mp[b]; } b *= R; } return -1; } ll bsgs_mat(mat A, mat B) { constexpr ll S = 70000; map mp; mat R = E; for (int i = 0; i < S; i++) { if (!mp.count(R)) { mp[R] = i; } R = mul(R, A); } // A^{50000i + j} = B // A^j = B^A{-50000i} R = inv(R); for (int i = 0; i < S; i++) { if (mp.count(B)) { return S*i + mp[B]; } B = mul(B, R); } return -1; } // ---------------------------------------------------------------- // det(A)=0 のときケーリーハミルトンの定理より A^2-(a+d)A = O が成り立つ。つまり // A^n = (a+d)^{n-1} A = B // これは BSGS で解ける。 // ---------------------------------------------------------------- // det(A)!=0 のとき。 // // det(A)^N = det(B) より N%T が求められる。ここで T は det(A) の位数とする。 // よって // A ^ {N%T + Tk} = B // det(A) != 0 なので A に逆行列があり // (A^T)^k = B^A^{-N%T} // // det(A^T)=1 なので A^T の周期は 2p 以下。これで BSGS が使える。 int main() { initio(); cin >> MOD; mat A(4), B(4); rep(i, 4) cin >> A[i]; rep(i, 4) cin >> B[i]; if (det(A) == 0) { int k = -1; rep(i, 4) if (A[i] != 0) k = i; if (k == -1) { if (A == B) { cout << 1 << endl; return 0; } cout << -1 << endl; return 0; } ll p = bsgs(A[0] + A[3], B[k] * modinv(A[k])); if (matpow(A, p) == B) { cout << p << endl; } else { cout << -1 << endl; } return 0; } ll p = bsgs_except_zero(det(A), det(B)); if (p == -1) { cout << -1 << endl; return 0; } ll T = bsgs_except_zero(det(A), 1); // (A^T)^k = B^A^{-N%T} ll k = bsgs_mat(matpow(A, T), mul(B, matpow(inv(A), p))); if (k == -1) { cout << -1 << endl; return 0; } cout << p + T*k << endl; }