#include #include using namespace std; using namespace atcoder; using ll = int64_t; using ull = uint64_t; using ld = long double; template void chmax(T& target, const V& cand) { target = max(target, static_cast(cand)); } template void chmin(T& target, const V& cand) { target = min(target, static_cast(cand)); } #define fae(type, var, from, to) \ for (type var = static_cast(from); var < static_cast(to); ++var) #define fai(type, var, from, to) \ for (type var = static_cast(from); var <= static_cast(to); ++var) #define fdi(type, var, from, to) \ for (type var = static_cast(from); var >= static_cast(to); --var) int main() { ll N, M; cin >> N >> M; vector> P[32]; P[0] = {{2, 1}, {1, 1}}; auto prod = [&](auto& a, auto& b) { auto c = a; c[0][0] = (a[0][0] * b[0][0] + a[0][1] * b[1][0]) % M; c[0][1] = (a[0][0] * b[0][1] + a[0][1] * b[1][1]) % M; c[1][0] = (a[1][0] * b[0][0] + a[1][1] * b[1][0]) % M; c[1][1] = (a[1][0] * b[0][1] + a[1][1] * b[1][1]) % M; return c; }; auto gen = [&](ll k) { vector> G{{1, 0}, {0, 1}}; ll i = 0; while (k > 0) { if (k & 1) { G = prod(P[i], G); } P[i + 1] = prod(P[i], P[i]); k = k >> 1; i++; } return G; }; auto k = (N - 1) / 2; auto G = gen(k); if (N % 2 == 0) { cout << (G[0][0] % M) << endl; } else { cout << (G[1][0] % M) << endl; } }