#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; #define all(v) v.begin(),v.end() #define erase_uq(v) v.erase(unique(v.begin(),v.end()),v.end()) ll mod; vector> matmul(vector> A, vector> B){ int N = A.size(); vector> ans(N, vector(N, 0)); for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ for (int k = 0; k < N; k++){ ans[i][k] += A[i][j] * B[j][k]; ans[i][k] %= mod; } } } return ans; } vector> matpow(vector> A, long long b){ int N = A.size(); vector> ans(N, vector(N, 0)); for (int i = 0; i < N; i++){ ans[i][i] = 1; } while (b > 0){ if (b % 2 == 1){ ans = matmul(ans, A); } A = matmul(A, A); b /= 2; } return ans; } int main(){ cin.tie(0); ios::sync_with_stdio(0); ll N; cin >> N >> mod; vector> A = {{1, 1}, {1, 0}}; A = matpow(A, N - 2); cout << A[0][0] << endl; }