// #define _GLIBCXX_DEBUG #include #include // g++ main.cpp -std=c++17 -I . using namespace std; using namespace atcoder; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() template inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;} template inline bool chmax(T&x,U y){if(x void my_printv(vector v,bool endline = true){ if(!v.empty()){ for(size_t i{}; i struct Matrix { int sz; vector> x; Matrix(int n) : sz(n) { x.assign(sz, vector (sz, (T)0)); } vector &operator [] (int i) { return x[i]; } Matrix operator*(const Matrix& other) const { Matrix C(sz); for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { for (int k = 0; k < sz; k++) { C.x[i][j] = (C.x[i][j] + x[i][k] * other.x[k][j])%m; } } } return C; } Matrix pow(long long n) { Matrix Res(sz); Matrix Pow = *this; for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) Res.x[i][j] = (i == j); } while (n > 0) { if (n & 1) Res = Res * Pow; Pow = Pow * Pow; n >>= 1; } return Res; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); Matrix mat(2); mat[0][0] = 1, mat[0][1] = 1, mat[1][0] = 1, mat[1][1] = 0; cin>>n>>m; auto ans = mat.pow(n-1)[1][0]; cout << ans << endl; // vector dp(2); // dp[0] = 0, dp[1] = 1; // rep(i, n-1) { // ll t = dp[1]; // dp[1] = (dp[0] + dp[1])%m; // dp[0] = t; // } // cout << dp[0] << endl; }