#include using namespace std; using lint = long long int; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector &vec, int len) { vec.resize(len); } template void ndarray(vector &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template istream &operator>>(istream &is, vector &vec){ for (auto &v : vec) is >> v; return is; } ///// This part below is only for debug, not used ///// template ostream &operator<<(ostream &os, const vector &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const deque &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const pair &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } template ostream &operator<<(ostream &os, const map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; ///// END ///// struct ModIntRuntime { using lint = long long int; static int get_mod() { return mod; } int val; static int mod; static vector &facs() { static vector facs_; return facs_; } static int &get_primitive_root() { static int primitive_root_ = 0; if (!primitive_root_) { primitive_root_ = [&](){ set fac; int v = mod - 1; for (lint i = 2; i * i <= v; i++) while (v % i == 0) fac.insert(i), v /= i; if (v > 1) fac.insert(v); for (int g = 1; g < mod; g++) { bool ok = true; for (auto i : fac) if (ModIntRuntime(g).power((mod - 1) / i) == 1) { ok = false; break; } if (ok) return g; } return -1; }(); } return primitive_root_; } static void set_mod(const int &m) { if (mod != m) facs().clear(); mod = m; get_primitive_root() = 0; } ModIntRuntime &_setval(lint v) { val = (v >= mod ? v - mod : v); return *this; } ModIntRuntime() : val(0) {} ModIntRuntime(lint v) { _setval(v % mod + mod); } explicit operator bool() const { return val != 0; } ModIntRuntime operator+(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val + x.val); } ModIntRuntime operator-(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val - x.val + mod); } ModIntRuntime operator*(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val * x.val % mod); } ModIntRuntime operator/(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val * x.inv() % mod); } ModIntRuntime operator-() const { return ModIntRuntime()._setval(mod - val); } ModIntRuntime &operator+=(const ModIntRuntime &x) { return *this = *this + x; } ModIntRuntime &operator-=(const ModIntRuntime &x) { return *this = *this - x; } ModIntRuntime &operator*=(const ModIntRuntime &x) { return *this = *this * x; } ModIntRuntime &operator/=(const ModIntRuntime &x) { return *this = *this / x; } friend ModIntRuntime operator+(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod + x.val); } friend ModIntRuntime operator-(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod - x.val + mod); } friend ModIntRuntime operator*(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod * x.val % mod); } friend ModIntRuntime operator/(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod * x.inv() % mod); } bool operator==(const ModIntRuntime &x) const { return val == x.val; } bool operator!=(const ModIntRuntime &x) const { return val != x.val; } bool operator<(const ModIntRuntime &x) const { return val < x.val; } friend istream &operator>>(istream &is, ModIntRuntime &x) { lint t; is >> t; x = ModIntRuntime(t); return is; } friend ostream &operator<<(ostream &os, const ModIntRuntime &x) { os << x.val; return os; } lint power(lint n) const { lint ans = 1, tmp = this->val; while (n) { if (n & 1) ans = ans * tmp % mod; tmp = tmp * tmp % mod; n /= 2; } return ans; } lint inv() const { return this->power(mod - 2); } ModIntRuntime operator^(lint n) const { return ModIntRuntime(this->power(n)); } ModIntRuntime &operator^=(lint n) { return *this = *this ^ n; } ModIntRuntime fac() const { int l0 = facs().size(); if (l0 > this->val) return facs()[this->val]; facs().resize(this->val + 1); for (int i = l0; i <= this->val; i++) facs()[i] = (i == 0 ? ModIntRuntime(1) : facs()[i - 1] * ModIntRuntime(i)); return facs()[this->val]; } ModIntRuntime doublefac() const { lint k = (this->val + 1) / 2; if (this->val & 1) return ModIntRuntime(k * 2).fac() / ModIntRuntime(2).power(k) / ModIntRuntime(k).fac(); else return ModIntRuntime(k).fac() * ModIntRuntime(2).power(k); } ModIntRuntime nCr(const ModIntRuntime &r) const { if (this->val < r.val) return ModIntRuntime(0); return this->fac() / ((*this - r).fac() * r.fac()); } ModIntRuntime sqrt() const { if (val == 0) return 0; if (mod == 2) return val; if (power((mod - 1) / 2) != 1) return 0; ModIntRuntime b = 1; while (b.power((mod - 1) / 2) == 1) b += 1; int e = 0, m = mod - 1; while (m % 2 == 0) m >>= 1, e++; ModIntRuntime x = power((m - 1) / 2), y = (*this) * x * x; x *= (*this); ModIntRuntime z = b.power(m); while (y != 1) { int j = 0; ModIntRuntime t = y; while (t != 1) j++, t *= t; z = z.power(1LL << (e - j - 1)); x *= z, z *= z, y *= z; e = j; } return ModIntRuntime(min(x.val, mod - x.val)); } }; int ModIntRuntime::mod = 1; template T determinant(vector> mtr) { return mtr[0][0] * mtr[1][1] - mtr[0][1] * mtr[1][0]; } template vector> matmul(const vector> &A, const vector> &B) { int H = A.size(), W = B[0].size(), K = B.size(); vector> C(H, vector(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = 0; k < K; k++) { C[i][j] += A[i][k] * B[k][j]; } } } return C; } template vector matmul(const vector> &A, const vector &v) { vector res(A.size()); for (int i = 0; i < (int)A.size(); i++) { for (int j = 0; j < (int)v.size(); j++) { res[i] += A[i][j] * v[j]; } } return res; } template vector> matpower(vector> X, lint n) { vector> res(X.size(), vector(X.size())); for (int i = 0; i < (int)res.size(); i++) res[i][i] = 1; while (n) { if (n & 1) res = matmul(res, X); X = matmul(X, X); n >>= 1; } return res; } using mint = ModIntRuntime; using Matrix = vector>; struct DiscreteLogarithm { using lint = long long int; int M, stepsize; lint baby_a, giant_a, g; std::unordered_map baby_log_dict; lint inverse(lint a) { lint b = M / g, u = 1, v = 0; while (b) { lint t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } u %= M / g; return u >= 0 ? u : u + M / g; } DiscreteLogarithm(int mod, int a_new) : M(mod), baby_a(a_new % mod), giant_a(1) { g = 1; while (std::__gcd(baby_a, M / g) > 1) g *= std::__gcd(baby_a, M / g); stepsize = 32; // lg(MAX_M) while (stepsize * stepsize < M / g) stepsize++; lint now = 1 % (M / g), inv_g = inverse(baby_a); for (int n = 0; n < stepsize; n++) { if (!baby_log_dict.count(now)) baby_log_dict[now] = n; (now *= baby_a) %= M / g; (giant_a *= inv_g) %= M / g; } } // log(): returns the smallest nonnegative x that satisfies a^x = b mod M, or -1 if there's no solution lint log(lint b) { b %= M; lint acc = 1 % M; for (int i = 0; i < stepsize; i++) { if (acc == b) return i; (acc *= baby_a) %= M; } if (b % g) return -1; // No solution lint now = b * giant_a % (M / g); for (lint q = 1; q <= M / stepsize + 1; q++) { if (baby_log_dict.count(now)) return q * stepsize + baby_log_dict[now]; (now *= giant_a) %= M / g; } return -1; } }; void NO() { puts("-1"); exit(0); } bool valid_check(Matrix A, Matrix B) { if (A[0][1] == 0 and B[0][1] != 0) NO(); if (A[1][0] == 0 and B[1][0] != 0) NO(); return true; } Matrix inverse(Matrix m) { mint det = determinant(m); return Matrix{{m[1][1] / det, -m[0][1] / det}, {-m[1][0] / det, m[0][0] / det}}; } int main() { int m; cin >> m; mint::set_mod(m); vector> A(2, vector(2)); vector> B(2, vector(2)); cin >> A >> B; FOR(i, 1, 100) if (matpower(A, i) == B) { cout << i << endl; return 0; } valid_check(A, B); if (A[0][1] == 0 and A[1][0] == 0) { // Aが対角行列:コーナーケース REP(d, 2) if ((A[d][d] == 0) ^ (B[d][d] == 0)) NO(); lint n0 = DiscreteLogarithm(m, A[0][0].val).log(B[0][0].val); lint n1 = DiscreteLogarithm(m, A[1][1].val).log(B[1][1].val); if (A[0][0] == 0 and A[1][1] == 0) NO(); else if (A[0][0] == 0) { cout << n1 << endl; } else if (A[1][1] == 0) { cout << n0 << endl; } else if (n0 < 0 or n1 < 0) NO(); else { lint m0 = DiscreteLogarithm(m, A[0][0].val).log((1 / A[0][0]).val) + 1; lint t1 = DiscreteLogarithm(m, A[1][1].power(m0)).log((B[1][1] / A[1][1].power(n0)).val); if (t1 < 0) NO(); else cout << n0 + m0 * t1 << endl; } return 0; } mint p = (A[0][1] !=0 ? B[0][1] / A[0][1] : B[1][0] / A[1][0]); mint q = B[0][0] - p * A[0][0]; if (A[1][1] * p + q != B[1][1] or A[1][0] * p != B[1][0] or A[0][1] * p != B[0][1]) NO(); Matrix trans{{A[0][0] + A[1][1], 1}, {A[0][1] * A[1][0] - A[0][0] * A[1][1], 0}}; if (trans[0][0] == 0 and trans[1][0] == 0) NO(); Matrix trans_inv(2, vector(2)); if (trans[1][0]) trans_inv = {{0, 1 / trans[1][0]}, {1, -trans[0][0] / trans[1][0]}}; const lint BN = 100000; if (trans[1][0] == 0) { mint v = 1; map ma; FOR(d, 1, BN + 2) { if (!ma.count(v)) ma[v] = d; v = trans[0][0] * v; } REP(e, BN) { mint tgt = p / (mint(trans[0][0]) ^ (BN * e)); if (ma.count(tgt)) { cout << ma[tgt] + e * BN << endl; return 0; } } NO(); } mint detA = determinant(A); lint i_det = DiscreteLogarithm(m, detA.val).log(determinant(B).val); if (i_det < 0) NO(); lint p_det = DiscreteLogarithm(m, detA.val).log((1 / detA).val) + 1; map ma; Matrix tmp = matpower(A, i_det); Matrix tmp_p = matpower(A, p_det); REP(i, BN + 2) { if (!ma.count(tmp) and i_det + p_det * i > 0) ma[tmp] = i_det + p_det * i; tmp = matmul(tmp, tmp_p); } REP(i, BN + 1) { Matrix q = matmul(inverse(matpower(tmp_p, i * BN)), B); if (ma.count(q)) { cout << ma[q] + i * BN * p_det << endl; return 0; } } NO(); }