#include // created [2019/12/12] 23:52:05 #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wsign-conversion" using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; using uint = unsigned int; using usize = std::size_t; using ll = long long; using ull = unsigned long long; using ld = long double; template using arr = T (&)[n]; template using c_arr = const T (&)[n]; template constexpr T popcount(const T u) { return u ? static_cast(__builtin_popcountll(static_cast(u))) : static_cast(0); } template constexpr T log2p1(const T u) { return u ? static_cast(64 - __builtin_clzll(static_cast(u))) : static_cast(0); } template constexpr T msbp1(const T u) { return log2p1(u); } template constexpr T lsbp1(const T u) { return __builtin_ffsll(u); } template constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast(u); } template constexpr bool ispow2(const T u) { return u and (static_cast(u) & static_cast(u - 1)) == 0; } template constexpr T ceil2(const T u) { return static_cast(1) << clog(u); } template constexpr T floor2(const T u) { return u == 0 ? static_cast(0) : static_cast(1) << (log2p1(u) - 1); } template constexpr bool btest(const T mask, const usize ind) { return static_cast((static_cast(mask) >> ind) & static_cast(1)); } template void bset(T& mask, const usize ind) { mask |= (static_cast(1) << ind); } template void breset(T& mask, const usize ind) { mask &= ~(static_cast(1) << ind); } template void bflip(T& mask, const usize ind) { mask ^= (static_cast(1) << ind); } template void bset(T& mask, const usize ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); } template constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast(0) : static_cast((static_cast(mask) << (64 - ind)) >> (64 - ind)); } template bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } constexpr unsigned int mod = 1000000007; template constexpr T inf_v = std::numeric_limits::max() / 4; template constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; template T in() { T v; return std::cin >> v, v; } template T in_v(typename std::enable_if<(i == n), c_arr>::type) { return in(); } template auto in_v(typename std::enable_if<(i < n), c_arr>::type& szs) { const usize s = (usize)szs[i]; std::vector(szs))> ans(s); for (usize j = 0; j < s; j++) { ans[j] = in_v(szs); } return ans; } template auto in_v(c_arr szs) { return in_v(szs); } template auto in_t() { return std::tuple...>{in()...}; } struct io_init { io_init() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(20); } } hogechan; template void out(const T& v) { std::cout << v << '\n'; } // Reactiveではstd::flushを忘れない template void out(const T& v, const Args... args) { std::cout << v << ' ', out(args...); } # define SHOW(...) static_cast(0) constexpr ull TEN(const usize n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; } template auto make_v(typename std::enable_if<(i == n), c_arr>::type, const T& v = T{}) { return v; } template auto make_v(typename std::enable_if<(i < n), c_arr>::type szs, const T& v = T{}) { const usize s = (usize)szs[i]; return std::vector(szs, v))>(s, make_v(szs, v)); } template auto make_v(c_arr szs, const T& t = T{}) { return make_v(szs, t); } template T gcd(const T& a, const T& b) { return a < 0 ? gcd(-a, b) : b < 0 ? gcd(a, -b) : (a > b ? gcd(b, a) : a == 0 ? b : gcd(b % a, a)); } template T lcm(const T& a, const T& b) { return a / gcd(a, b) * b; } template constexpr std::pair extgcd(const T a, const T b) { if (b == 0) { return std::pair{1, 0}; } const auto g = gcd(a, b), da = std::abs(b) / g; const auto p = extgcd(b, a % b); const auto x = (da + p.second % da) % da, y = (g - a * x) / b; return {x, y}; } template constexpr T inverse(const T a, const T mod) { return extgcd(a, mod).first; } template class modint_base { public: template static std::enable_if_t mod() { return mod_ref(); } template static constexpr std::enable_if_t mod() { return mod_value; } template static void set_mod(const std::enable_if_t mod) { mod_ref() = mod, inv_ref() = {1, 1}; } modint_base() : v{0} {} modint_base(const ll val) : v{norm(static_cast(val % static_cast(mod()) + static_cast(mod())))} {} modint_base(const modint_base& n) : v{n()} {} explicit operator bool() const { return v != 0; } bool operator!() const { return not static_cast(*this); } modint_base& operator=(const modint_base& m) { return v = m(), (*this); } modint_base& operator=(const ll val) { return v = norm(uint(val % static_cast(mod()) + static_cast(mod()))), (*this); } friend modint_base operator+(const modint_base& m) { return m; } friend modint_base operator-(const modint_base& m) { return make(norm(mod() - m.v)); } friend modint_base operator+(const modint_base& m1, const modint_base& m2) { return make(norm(m1.v + m2.v)); } friend modint_base operator-(const modint_base& m1, const modint_base& m2) { return make(norm(m1.v + mod() - m2.v)); } friend modint_base operator*(const modint_base& m1, const modint_base& m2) { return make(static_cast(static_cast(m1.v) * static_cast(m2.v) % static_cast(mod()))); } friend modint_base operator/(const modint_base& m1, const modint_base& m2) { return m1 * inv(m2.v); } friend modint_base operator+(const modint_base& m, const ll val) { return modint_base{static_cast(m.v) + val}; } friend modint_base operator-(const modint_base& m, const ll val) { return modint_base{static_cast(m.v) - val}; } friend modint_base operator*(const modint_base& m, const ll val) { return modint_base{static_cast(m.v) * (val % static_cast(mod()))}; } friend modint_base operator/(const modint_base& m, const ll val) { return modint_base{static_cast(m.v) * inv(val)}; } friend modint_base operator+(const ll val, const modint_base& m) { return modint_base{static_cast(m.v) + val}; } friend modint_base operator-(const ll val, const modint_base& m) { return modint_base{-static_cast(m.v) + val}; } friend modint_base operator*(const ll val, const modint_base& m) { return modint_base{static_cast(m.v) * (val % static_cast(mod()))}; } friend modint_base operator/(const ll val, const modint_base& m) { return modint_base{val * inv(static_cast(m.v))}; } friend modint_base& operator+=(modint_base& m1, const modint_base& m2) { return m1 = m1 + m2; } friend modint_base& operator-=(modint_base& m1, const modint_base& m2) { return m1 = m1 - m2; } friend modint_base& operator*=(modint_base& m1, const modint_base& m2) { return m1 = m1 * m2; } friend modint_base& operator/=(modint_base& m1, const modint_base& m2) { return m1 = m1 / m2; } friend modint_base& operator+=(modint_base& m, const ll val) { return m = m + val; } friend modint_base& operator-=(modint_base& m, const ll val) { return m = m - val; } friend modint_base& operator*=(modint_base& m, const ll val) { return m = m * val; } friend modint_base& operator/=(modint_base& m, const ll val) { return m = m / val; } friend modint_base operator^(const modint_base& m, const ll n) { return power(m.v, n); } friend modint_base& operator^=(modint_base& m, const ll n) { return m = m ^ n; } friend bool operator==(const modint_base& m1, const modint_base& m2) { return m1.v == m2.v; } friend bool operator!=(const modint_base& m1, const modint_base& m2) { return not(m1 == m2); } friend bool operator==(const modint_base& m, const ll val) { return m.v == norm(static_cast(static_cast(mod()) + val % static_cast(mod()))); } friend bool operator!=(const modint_base& m, const ll val) { return not(m == val); } friend bool operator==(const ll val, const modint_base& m) { return m.v == norm(static_cast(static_cast(mod()) + val % static_cast(mod()))); } friend bool operator!=(const ll val, const modint_base& m) { return not(m == val); } friend std::istream& operator>>(std::istream& is, modint_base& m) { ll v; return is >> v, m = v, is; } friend std::ostream& operator<<(std::ostream& os, const modint_base& m) { return os << m(); } uint operator()() const { return v; } static modint_base small_inv(const usize n) { auto& in = inv_ref(); if (n < in.size()) { return in[n]; } for (usize i = in.size(); i <= n; i++) { in.push_back(-in[modint_base::mod() % i] * (modint_base::mod() / i)); } return in.back(); } private: template static std::enable_if_t mod_ref() { static UInt mod = 0; return mod; } static uint norm(const uint x) { return x < mod() ? x : x - mod(); } static modint_base make(const uint x) { modint_base m; return m.v = x, m; } static modint_base power(modint_base x, ull n) { modint_base ans = 1; for (; n; n >>= 1, x *= x) { if (n & 1) { ans *= x; } } return ans; } static modint_base inv(const ll v) { return v < 1000000 ? small_inv(static_cast(v)) : modint_base{inverse(v, static_cast(mod()))}; } static std::vector& inv_ref() { static std::vector in{1, 1}; return in; } uint v; }; template using modint = modint_base; template using dynamic_modint = modint_base; template std::vector divisors(const T n) { std::vector head, tail; for (T i = 1; i * i <= n; i++) { if (n % i == 0) { head.push_back(i); if (i * i != n) { tail.push_back(n / i); } } } for (auto it = tail.rbegin(); it != tail.rend(); it++) { head.push_back(*it); } return head; } template std::vector> prime_factors(T n) { std::vector> ans; for (T i = 2; i * i <= n; i++) { usize cnt = 0; for (; n % i == 0; n /= i, cnt++) {} if (cnt > 0) { ans.push_back({i, cnt}); } } if (n > 1) { ans.push_back({n, 1}); } return ans; } template ull period(const modint_base& x) { using mint = modint_base; const ull p = mint::mod(); // constexprにしたいね const auto ds = divisors(p - 1); for (const ull d : ds) { if ((x ^ d) == 1) { return d; } } return p - 1; } template mint primal_root() { const ull p = mint::mod(); // constexprにしたいね const auto fs = prime_factors(p - 1); mint g = 1; for (;; g += 1) { bool ok = true; for (const auto& q : fs) { const ull per = (p - 1) / q.first; if ((g ^ per) == 1) { ok = false; break; } } if (ok) { return g; } } return p; } template std::pair crt(const InIt first, const InIt last) { using P = std::pair; return std::accumulate(first, last, P{0, 1}, [](const P& a1, const P& a2) -> P { T r1 = a1.first, m1 = a1.second, r2 = a2.first, m2 = a2.second; const T g = gcd(m1, m2); if (r1 % g != r2 % g) { return P{0, 0}; } const T m = m1 / g * m2; if (r1 == r2) { return {r1, m}; } const auto k1 = extgcd(m1, m2).first * ((r2 - r1) / g) % m; return P{(m + m1 * k1 % m + r1) % m, m}; }); } template class discrete_log { public: discrete_log(const Ring x, const ull period) : x{x}, period{period} { for (; bs * bs < period; bs++) {} for (ull i = 0; i * bs < period; i++) { giant[x ^ (i * bs)] = i * bs; } } ull operator()(Ring y) { for (ull i = 0; i < bs; i++) { if (giant.count(y)) { return (giant[y] + period - i) % period; } y = y * x; } return period; } private: const Ring x; ull period; ull bs = 1; std::map giant; }; using mint = dynamic_modint<0>; bool operator<(const mint& m1, const mint& m2) { return m1() < m2(); } mint g; template struct mat { mat() { table[0][0] = T{0}; table[0][1] = T{0}; table[1][0] = T{0}; table[1][1] = T{0}; } mat(const T& a00, const T& a01, const T& a10, const T& a11) { table[0][0] = a00; table[0][1] = a01; table[1][0] = a10; table[1][1] = a11; } std::array& operator[](const int i) { return table[i]; } const std::array& operator[](const int i) const { return table[i]; } friend bool operator==(const mat& m1, const mat& m2) { return m1.table == m2.table; } friend mat operator+(const mat& m1, const mat& m2) { mat ans; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { ans[i][j] = m1[i][j] + m2[i][j]; } } return ans; } friend mat operator-(const mat& m1, const mat& m2) { mat ans; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { ans[i][j] = m1[i][j] - m2[i][j]; } } return ans; } friend mat operator*(const mat& m1, const mat& m2) { mat ans; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { ans[i][j] = ans[i][j] + m1[i][k] * m2[k][j]; } } } return ans; } friend mat operator*(const mat& m, const T& v) { mat ans; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { ans[i][j] = m[i][j] * v; } } return ans; } friend mat operator^(const mat& m, const ll k) { return k == 0 ? I() : k % 2 == 0 ? (m * m) ^ (k / 2) : (m ^ (k - 1)) * m; } friend std::ostream& operator<<(std::ostream& os, const mat& m) { os << "["; for (int i = 0; i < 2; i++) { os << "["; for (int j = 0; j < 2; j++) { os << m[i][j] << ","; } os << "]\n"; } return os << "]\n"; } static mat I() { mat ans; ans[0][0] = T{1}, ans[1][1] = T{1}; return ans; } static T tr(const mat& m) { return m[0][0] + m[1][1]; } static T det(const mat& m) { return m[0][0] * m[1][1] - m[0][1] * m[1][0]; } static mat inv(const mat& m) { const T d = det(m); mat ans; ans[0][0] = m[1][1] / d; ans[0][1] = -m[0][1] / d; ans[1][0] = -m[1][0] / d; ans[1][1] = m[0][0] / d; return ans; } std::array, 2> table; }; struct C { C() : x{0}, y{0} {} C(const mint& x) : x{x}, y{0} {} C(const mint& x, const mint& y) : x{x}, y{y} {} friend bool operator==(const C& c1, const C& c2) { return c1.x == c2.x and c1.y == c2.y; } friend bool operator!=(const C& c1, const C& c2) { return not(c1 == c2); } friend bool operator<(const C& c1, const C& c2) { return c1.x != c2.x ? c1.x < c2.x : c1.y < c2.y; } friend C operator-(const C& c) { return C{-c.x, -c.y}; } friend C operator+(const C& c1, const C& c2) { return C{c1.x + c2.x, c1.y + c2.y}; } friend C operator-(const C& c1, const C& c2) { return C{c1.x - c2.x, c1.y - c2.y}; } friend C operator*(const C& c1, const C& c2) { return C{c1.x * c2.x + g * c1.y * c2.y, c1.x * c2.y + c1.y * c2.x}; } friend C operator/(const C& c1, const C& c2) { return c1 * conj(c2); } friend C operator*(const C& c, const mint& v) { return C{c.x * v, c.y * v}; } friend C operator/(const C& c, const mint& v) { return C{c.x / v, c.y / v}; } friend C operator^(const C& c, const ll k) { const mint a = c.x; const mint b = c.y; const auto m = (mat(a, b * g, b, a)) ^ k; return C{m[0][0], m[1][0]}; } static mint normSq(const C& c) { return c.x * c.x - g * c.y * c.y; } static C conj(const C& c) { return C{c.x, -c.y} / normSq(c); } friend std::ostream& operator<<(std::ostream& os, const C& c) { return os << c.x << "+" << c.y << "\\sqrt(g)"; } mint x, y; // x+y\sqrt{g} }; int main() { const uint p = in(); mint::set_mod(p); g = primal_root(); const auto as = in_v({4}); const auto bs = in_v({4}); mat A(as[0], as[1], as[2], as[3]); mat B(bs[0], bs[1], bs[2], bs[3]); for (int i = 1; i <= 100; i++) { if ((A ^ i) == B) { return std::cout << i << std::endl, 0; } } discrete_log pr_dlog(g, p - 1); auto sqrt = [&](const mint x) -> C { if (x == 0) { return {0, 0}; } const ll y = pr_dlog(x); if (y % 2 == 0) { return {g ^ (y / 2), 0}; } else { return {0, g ^ ((y - 1) / 2)}; } }; auto eigens = [&](const mat& A) -> std::array { const auto s = mat::tr(A); const auto t = mat::det(A); const auto d = s * s - 4 * t; const auto sq = sqrt(d); return {(C{s, 0} + sq) / 2, (C{s, 0} - sq) / 2}; }; auto eigen_vecs = [&](const auto& A) -> mat { // D=S^(-1)ASのS if (A[0][1] == 0 and A[1][0] == 0) { return mat(C{1}, C{0}, C{0}, C{1}); } const auto es = eigens(A); if (es[0] == es[1]) { // ジョルダン標準形 const mint l = es[0].x; const auto N = A - mat::I() * l; if (N[0][1] != 0 or N[1][1] != 0) { return mat(C{N[0][1]}, C{0}, C{N[1][1]}, C{1}); } else { return mat(C{N[0][0]}, C{1}, C{N[1][0]}, C{0}); } } else { // 対角化可能 std::array, 2> ans; for (int i = 0; i < 2; i++) { const std::array v1{{C{A[0][0], 0} - es[i], C{A[0][1], 0}}}; ans[i] = {{{-A[0][1], 0}, v1[0]}}; } return mat(ans[0][0], ans[1][0], ans[0][1], ans[1][1]); } }; auto normal = [&](const C& c) { if (c == C{0}) { return c; } if (c.y == 0) { return C{1, 0}; } return C{c.x / c.y, 1}; }; SHOW(eigens(A)); SHOW(eigens(B)); const auto AS = eigen_vecs(A); SHOW(AS); mat CA(C{A[0][0]}, C{A[0][1]}, C{A[1][0]}, C{A[1][1]}); mat CB(C{B[0][0]}, C{B[0][1]}, C{B[1][0]}, C{B[1][1]}); CA = mat::inv(AS) * CA * AS; CB = mat::inv(AS) * CB * AS; assert(CA[1][0] == C{0}); SHOW(CA, CB); // ここまでが間違っていたらマジで終了 auto lg = [&](const C& c1, const C& c2) -> std::pair { // c=a+b\sqrt{g}について // c^(p+1) = a^2-(b^2)g (特にF_pの元) if (c1 == C{0}) { if (c2 == C{0}) { return {0, 1}; } else { return {0, 0}; } } if (c2 == C{0}) { return {0, 0}; } static std::vector ds; if (ds.empty()) { ds = divisors(p + 1); } ll per = p + 1; for (const ll d : ds) { if ((c1 ^ d).y == 0) { per = d; break; } } const mint c = c2.x; const mint d = c2.y; const mint n = (c1 ^ per).x; assert(n != 0); SHOW(n); if (d == 0) { const ull per2 = period(n); const ull lg2 = discrete_log(n, per2)(c); return {lg2 * per, per * per2}; } else { auto res = [&]() { ll bs = 1; for (; bs * bs < per; bs++) {} std::map giant; for (ll i = 0; i * bs < per; i++) { const C n = normal(c1 ^ (i * bs)); giant[n] = i * bs; } C c2_ = normal(c2); for (ll i = 0; i < bs; i++) { if (giant.count(c2_)) { return (per + giant[c2_] - i) % per; } c2_ = normal(c2_ * c1); } return per; }(); SHOW(res, per); if (res == per) { return {0, 0}; } const C cr = c1 ^ res; const C d = c2 / cr; SHOW(d); assert(d.y == 0); const mint r = d.x; const ull per2 = period(n); const ull lg2 = discrete_log(n, per2)(r); return {lg2 * per + res, per2 * per}; } }; if (CB[1][0] != C{0}) { return std::cout << -1 << std::endl, 0; } if (CB[0][1] != C{0} and (CB[0][0] != CB[1][1] or CB[0][0].y != 0)) { return std::cout << -1 << std::endl, 0; } if ((CA[0][1] == C{0}) != (CB[0][1] == C{0})) { return std::cout << -1 << std::endl, 0; } if (CA[0][1] == C{0}) { // 対角 const auto p1 = lg(CA[0][0], CB[0][0]); const auto p2 = lg(CA[1][1], CB[1][1]); if (p1.second == 0 or p2.second == 0) { return std::cout << -1 << std::endl, 0; } std::vector> mods{p1, p2}; const auto ans = crt(mods.begin(), mods.end()); ll n = ans.first; if (n == 0) { n = ans.second; } std::cout << ((A ^ n) == B ? n : -1LL) << std::endl; } else { // 非対角 const mint l = CA[0][0].x; const mint m = (CB[0][1].x * l) / CB[0][0].x; const auto p1 = lg(CA[0][0], CB[0][0]); const auto p2 = std::pair{(ll)m(), (ll)p}; if (p1.second == 0 or p2.second == 0) { return std::cout << -1 << std::endl, 0; } std::vector> mods{p1, p2}; const auto ans = crt(mods.begin(), mods.end()); ll n = ans.first; if (n == 0) { n = ans.second; } std::cout << ((A ^ n) == B ? n : -1LL) << std::endl; } return 0; }