#include using namespace std; using ll = long long; using ull = unsigned long long; const int mod = 998244353; #include mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count()); int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}; int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; template bool chmin(T& a, const T& b){ if (b < a){ a = b; return true; } else { return false; } } template bool chmax(T& a, const T& b){ if (a < b){ a = b; return true; } else { return false; } } template struct Matrix{ vector> identity(int n){ vector> vec(n, vector (n)); for (int i = 0; i < n; i++){ vec[i][i] = T(1); } return vec; } //新しい操作を左に持ってくる vector> multiply(const vector>& a, const vector>& b){ int n = a.size(); int t = a[0].size(); int m = b[0].size(); vector> vec(n, vector (m, 0)); for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ for (int k = 0; k < t; k++){ T A = a[i][k]; T B = b[k][j]; T x = A * B; vec[i][j] += x; } } } return vec; } vector> matrixpow(ll k, const vector>& x, const vector>& y){ int n = x.size(); vector> id = identity(n); vector> c = x; while(k){ if (k & 1){ id = multiply(id, c); } auto d = multiply(c, c); c = d; k >>= 1; } auto ans = multiply(y, id); return ans; } }; void solve(){ int n, m; cin >> n >> m; Matrix mx; vector> id = mx.identity(2); vector> c(2, vector (2, 1)); c[1][1] = 0; int k = n - 2; while(k){ if (k & 1){ id = mx.multiply(id, c); } for (int i = 0; i < 2; i++){ for (int j = 0; j < 2; j++){ id[i][j] %= m; } } auto d = mx.multiply(c, c); for (int i = 0; i < 2; i++){ for (int j = 0; j < 2; j++){ d[i][j] %= m; } } c = d; k >>= 1; } vector> a(1, vector (2)); a[0][0]++; auto ans = mx.multiply(a, id); cout << ans[0][0] << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; while(t--){ cout << fixed << setprecision(15); solve(); } }