#pragma GCC optimize ("O3") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); } template using V = vector; template using VV = V>; struct rng { struct A { int n; const bool operator!=(A r) { return n != r.n; } A& operator++() { n++; return *this; } int operator*() { return n; } }; int l, r; rng(int r) : l(0), r(max(0, r)) {} rng(int l, int r) : l(l), r(max(l, r)) {} A begin() { return A{l}; } A end() { return A{r}; } }; const int MD = TEN(9) + TEN(8) + 10; uint md; ull imd; inline ull nx(ull x) { ull y = ull(x) * (x) - 2; ull d = (__int128(y) * imd) >> 64; return y - d * md; } ull naive(ll n) { ull s = 4; for (int i = 0; i < n; i++) { s = nx(s); } return s; } ull big(ull n) { ull s = 4; for (int i = 0; i < 100; i++) s = nx(s); ull t = s; n -= 100; uint c = 0; uint d = 0; static const uint B = TEN(8) / 2; V buf(B); while (c < n) { buf[d] = s; s = nx(s); c++; d++; if (d == B) d = 0; if (__builtin_expect(s == t, 0)) { n %= c; if (c - n < B) { return buf[(d + B - (c - n)) % B]; } c = 0; break; } } while (c < n) { s = nx(s); c++; } return s; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20); ll n; cin >> n >> md; if (md == 2) { cout << 0 << endl; return 0; } imd = ((__int128(1)<<64) + md-1) / md; ull ans; if (n < 1000) ans = naive(n); else ans = big(n); ans = (ans + ull(10) * md - 2) % md; cout << ans << endl; return 0; }