#include using namespace std; typedef long long ll; using vi=vector; using vll=vector; using vii=vector>; using pii=pair; using vvpii=vector>>; #define all(x) x.begin(),x.end() #define rep0(i,a) for(int i=0;i inline bool chmin(T& a, T b) { if (a > b) {a = b;return true;}return false;} template inline bool chmax(T& a, T b) { if (a < b) {a = b;return true;}return false;} // const long long inf = 1LL<<60; const int INF = (1<<30); // const int MOD=1000000007; // const int MOD=10000000007; template struct Kitamasa{ vector a; // 初期値ベクトル vector d; // 係数ベクトル int k; ll M; Kitamasa(vector& a, vector& d,ll M) : a(a), d(d), k((int)a.size()) {} // a_n の係数ベクトルを求める vector dfs(int64_t n){ if(n == k) return d; vector res(k); if(n & 1 || n < k * 2){ vector x = dfs(n - 1); for(int i = 0; i < k; ++i) res[i] = (d[i]%M)*(x[k - 1]%M)%M; for(int i = 0; i + 1 < k; ++i) res[i + 1] += (x[i]%M); } else{ vector> x(k, vector(k)); x[0] = dfs(n >> 1); for(int i = 0; i + 1 < k; ++i){ for(int j = 0; j < k; ++j) x[i + 1][j] = (d[j]%M) * (x[i][k - 1]%M)%M; for(int j = 0; j + 1 < k; ++j) x[i + 1][j + 1] += (x[i][j]%M); } for(int i = 0; i < k; ++i){ for(int j = 0; j < k; ++j){ res[j] += (x[0][i]%M) * (x[i][j]%M)%M; } } } return res; } // a_n を求める。nは0-index T calc(int64_t n){ vector x = dfs(n); T res = 0; for(int i = 0; i < k; ++i) res += x[i] * a[i]%M; return res; } }; int main(){ ll N,M; cin>>N>>M; N--; vll a={0,1}; vll d={1,1}; Kitamasa k(a,d,M); int t_res=k.calc(N); t_res%=M; cout<