#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* typedef vector vi; typedef vector vvi; typedef pair pii; typedef vector vpii; */ typedef long long ll; typedef vector vll; typedef vector vvll; typedef pair pll; typedef vector vpll; typedef long double ld; typedef vector vld; typedef vector vb; #define rep(i, n) for (ll i = 0; i < (n); i++) #define reps(i, n) for (ll i = 1; i <= (n); i++) #define rrep(i, n) for (ll i = (n) - 1; i >= 0; i--) #define rreps(i, n) for (ll i = (n); i >= 1; i--) #define all(v) (v).begin(), (v).end() template void chmin(T& a, T b) { a = min(a, b);} template void chmax(T& a, T b) { a = max(a, b);} constexpr int INF = 1 << 30; constexpr ll INFL = 1LL << 60; //constexpr ll MOD = 1000000007; constexpr ld EPS = 1e-12; ld PI = acos(-1.0); ll MOD; struct mint { ll x; mint(ll x=0):x((x%MOD+MOD)%MOD){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) {if ((x+=a.x)>=MOD) x-=MOD; return *this;} mint& operator-=(const mint a) {if ((x+=MOD-a.x)>=MOD) x-=MOD; return *this;} mint& operator*=(const mint a) {(x*=a.x)%=MOD; return *this;} mint operator+(const mint a) const {mint res(*this); return res+=a;} mint operator-(const mint a) const {mint res(*this); return res-=a;} mint operator*(const mint a) const {mint res(*this); return res*=a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // For prime mod. // Do not use if MOD is not prime number !! mint inv() const { return pow(MOD-2);} mint& operator/=(const mint a) { return (*this) *= a.inv();} mint operator/(const mint a) const {mint res(*this); return res/=a;} }; typedef vector vm; struct Matrix { int n; vector mat; Matrix(int n) : n(n), mat(vector(n, vm(n))) {} Matrix(vector& a) : n(a.size()), mat(vector(n, vm(n))) { rep(i, n) rep(j, n) mat[i][j] = a[i][j]; } Matrix matmul(const Matrix& a) const{ Matrix res(n); rep(i, n) rep(j, n) rep(k, n) res.mat[i][j] += mat[i][k] * a.mat[k][j]; return res; } Matrix pow(ll k) const{ Matrix res(n); if(!k) { rep(i, n) res.mat[i][i] = 1; return res; } res = pow(k >> 1); res = res.matmul(res); if(k & 1) res = matmul(res); return res; } }; void solve() { ll n; cin >> n >> MOD; vvll a(2, vll(2)); a[0][0] = a[0][1] = a[1][0] = 1; Matrix m(a); m = m.pow(n - 2); cout << m.mat[0][0].x << endl; return; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); }