#include #define rep(i,n) for(int i=0;i<(int)n;i++) using namespace std; using ll = long long; using P = pair; using vec = vector; using mat = vector; ll MOD; int msize; // 遷移行列のサイズ // DPの更新 vec matmul(vec &dp, mat &mt) { vec ret(msize, 0); rep(i, msize) { rep(j, msize) { ret[i] += mt[i][j] * dp[j] % MOD; ret[i] %= MOD; } } return ret; } // 遷移行列の更新(2乗) mat update(mat &mt) { mat ret(msize, vec(msize, 0)); rep(i, msize) { rep(j, msize) { rep(k, msize) { ret[i][j] += mt[i][k] * mt[k][j] % MOD; ret[i][j] %= MOD; } } } return ret; } // mt ** k * dp void matpow(vec &dp, mat &mt, int k) { msize = dp.size(); while(k) { if (k & 1) dp = matmul(dp, mt); mt = update(mt); k /= 2; } } int main() { ll n, m; cin >> n >> m; MOD = m; vec dp(2); mat mt(2, vec(2)); dp[0] = 0, dp[1] = 1; mt[0][0] = mt[0][1] = mt[1][0] = 1, mt[1][1] = 0; matpow(dp, mt, n); cout << dp[1] << endl; return 0; }