#include using namespace std; #define ll long long ll mod; // 行列積 vector> mat_mul(vector> &a, vector> &b) { vector> ret((int)a.size(), vector((int)b[0].size())); for (int i = 0; i < (int)a.size(); i++) { for (int j = 0; j < (int)b[0].size(); j++) { for (int k = 0; k < (int)b.size(); k++) { ret[i][j] += a[i][k] * b[k][j]; ret[i][j] %= mod; } } } return ret; } // 行列累乗 vector> mat_pow(vector> a, long long n) { vector> ret((int)a.size(), vector((int)a.size())); // 単位行列で初期化 for (int i = 0; i < (int)a.size(); i++) { ret[i][i] = 1; } // 繰り返し二乗法 while (n > 0) { if (n & 1) { ret = mat_mul(a, ret); } a = mat_mul(a, a); n >>= 1; } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; mod = m; vector> X(2, vector(2)); X[0][0] = 1; X[0][1] = 1; X[1][0] = 1; X[1][1] = 0; vector> Y(2, vector(1)); Y[0][0] = 1; Y[1][0] = 0; auto X_POW = mat_pow(X, n - 2); auto ans = mat_mul(X_POW, Y); cout << ans[0][0] << '\n'; return 0; }