#include #include #include #include #include #include #include using namespace std; int main() { int64_t n, m; cin >> n >> m; int64_t a = min((int64_t)10000, n); int64_t k = 2; for (int i = 0; i < a; i++) { k = k * (k + 4) % m; } if (n == a) { cout << k << endl; quick_exit(0); } int64_t c[1100] = {}; int64_t l = k, p = 0; for (int i = 0;; i++) { if (i % (1 << 20) == 0) { c[i >> 20] = k; } k = k * (k + 4) % m; if (k == l) { p = i + 1; break; } } int h = (n - a) % p; k = c[h >> 20]; h %= 1 << 20; //cout << h <<' '<