#define _GLIBCXX_DEBUG // test only #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; long long m; cin >> n >> m; vector> mat(2,vector(2,1)); mat[0][1] = 0; mat[1][0] = 0; vector> ks(2,vector(2,1)); ks[1][1] = 0; int left = n-2; while (left > 0) { if (left%2 == 1) { vector> nmat(2,vector(2,0)); for (int i=0;i<2;i++) { for (int j=0;j<2;j++) { for (int k=0;k<2;k++) { nmat[i][j] += mat[i][k]*ks[k][j]; nmat[i][j] %= m; } } } mat = nmat; } left /= 2; vector> nks(2,vector(2,0)); for (int i=0;i<2;i++) { for (int j=0;j<2;j++) { for (int k=0;k<2;k++) { nks[i][j] += ks[i][k]*ks[k][j]; nks[i][j] %= m; } } } ks = nks; } cout << mat[0][0] << endl; }