#include #define rep(i,n) for(int i=0;i<(n);++i) using namespace std; using ll = long long; //行列の掛け算 vector> mat_mul(vector> &a, vector> &b, ll mod) { vector> 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]) %= mod; } } } return res; } // 行列累乗 vector> mat_pow(vector> a, ll k, ll mod) { int N = a.size(); vector> res(N, vector(N)); rep(i,N) res[i][i] = 1; while (k>0) { if(k&1) res = mat_mul(a, res, mod); a = mat_mul(a, a, mod); k >>= 1; } return res; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll n,m; cin>>n>>m; vector> a = {{1,1},{1,0}}; a = mat_pow(a, n-1, m); ll ans = a[1][0]; cout << ans << endl; }