#include using namespace std; #define int long long #define rep(i,n) for(int i=0;i<(int)(n);i++) #define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define all(x) (x).begin(),(x).end() #define pb push_back #define mp make_pair #define mt make_tuple typedef pair pii; typedef vector vi; typedef vector vvi; const int inf = 1LL<<60; const int mod = 1e9 + 7; const double eps = 1e-9; /*{ }*/ int n, m; typedef vector vec; typedef vector mat; mat mul(mat &a, mat &b) { mat c(a.size(), vec(b[0].size())); for(int i = 0; i < a.size(); i++){ for(int k = 0; k < b.size(); k++){ for(int j = 0; j < b[0].size(); j++){ c[i][j] = (c[i][j] + (a[i][k] * b[k][j])) % m; } } } return c; } mat pow(mat a, int n) { mat b(a.size(), vec(a.size())); for(int i = 0; i < b.size(); i++) b[i][i] = 1; while(n > 0){ if(n & 1) b = mul(b, a); a = mul(a, a); n >>= 1; } return b; } signed main() { cin >> n >> m; mat a(1, vec(2)); a[0][0] = 1; mat b(2, vec(2)); b[0][0] = b[0][1] = b[1][0] = 1; b = pow(b, n-2); cout << mul(b, a)[0][0] << endl; return 0; }