#include using namespace std; #define mat vector> mat matmul(mat &a, mat &b, long long M) { mat res(a.size(), (vector)b[0].size()); for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b[0].size(); j++) { for (int k = 0; k < b.size(); k++) { (res[i][j] += a[i][k] * b[k][j]) %= M; } } } return res; } mat matpow(mat &a, long long n, long long M) { mat res(a.size(), (vector)a.size()); for (int i = 0; i < a.size(); i++) { res[i][i] = 1; } while (n > 0) { if (n & 1) res = matmul(a, res, M); a = matmul(a, a, M); n >>= 1; } return res; } int main() { long long N, M; cin >> N >> M; mat res(2, (vector)2); res = {{1, 1}, {1, 0}}; res = matpow(res, N - 3, M); cout << (res[0][0] + res[0][1]) % M << endl; }