#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define repd(i,a,b) for (int i=(a);i<(b);i++) #define rep(i,n) repd(i,0,n) #define all(x) (x).begin(),(x).end() template 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; } typedef long long ll; const long long INF = 1LL << 60; typedef pair P; typedef pair p; typedef vector vec; using Graph = vector>; using graph = vector>; const int inf = 1e9+7; const long long MOD = 1000000007; int M; int m;//遷移行列のサイズ //dpの更新 vec matmul(vec& dp, Graph& G) { vec ret(m, 0); rep(i, m) { rep(j, m) { ret[i] += (G[i][j] * dp[j])%M; } ret[i] %= M; } return ret; } //遷移行列の更新 Graph update(Graph& G) { Graph ret(m, vec(m, 0)); rep(i, m) { rep(j, m) { rep(k, m)ret[i][j] += (G[i][k] * G[k][j])%M; ret[i][j] %= M; } } return ret; } void matpow(vec& dp, Graph& G, int k) { m = dp.size(); while (k) { if (k & 1)dp = matmul(dp, G); G = update(G); k /= 2; } } int main() { int n; cin >> n >> M; vec dp{ 1,0 }; Graph G(2, vec(2)); G[0][0] = 1; G[0][1] = 1; G[1][0] = 1; G[1][1] = 0; matpow(dp, G, n-1); cout << dp[1] << endl; return 0; }