#include #include #include #include #include #include #include using namespace std; using ll = long long int; long long int N, MOD; // ModInt begin ll mod_pow(ll x, ll n) {return (!n)?1:(mod_pow((x*x)%MOD,n/2)*((n&1)?x:1))%MOD;} struct ModInt { ll v; ModInt(ll a = 0) : v(((a%MOD) + MOD) % MOD) {} ModInt operator+ ( const ModInt& b ) const {return (v + b.v) % MOD;} ModInt operator- ( const ModInt& b ) const {return (v - b.v + MOD) % MOD;} ModInt operator* ( const ModInt& b ) const {return (v * b.v) % MOD;} ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, MOD-2)) % MOD;} }; bool operator==(ModInt a, ModInt b) {return a.v == b.v;} ModInt& operator+=(ModInt& a, ModInt b) {return a = a + b;} ModInt& operator-=(ModInt& a, ModInt b) {return a = a - b;} ModInt& operator*=(ModInt& a, ModInt b) {return a = a * b;} ModInt& operator/=(ModInt& a, ModInt b) {return a = a / b;} ostream& operator<<(ostream& out, ModInt a) {return out << a.v;} istream& operator>>(istream& in, ModInt& a) { ll v; in >> v; a = ModInt(v); return in; } // ModInt end template struct Matrix { private: int R, C; public: std::vector< std::vector > mat; Matrix (int R_, int C_, bool is_identity=false) { R = R_, C = C_; mat = std::vector< std::vector >(R, std::vector(C)); if(is_identity) { assert(R == C); for(int i=0; i 0); return mat[0].size(); } std::vector& operator[](int x) { return mat[x]; } Matrix& operator=(const Matrix &a) { mat = a.mat; return *this; } Matrix operator*(Matrix a) { assert(this->get_col() == a.get_row()); int R = this->get_row(), C = a.get_col(), X = this->get_col(); Matrix ret(R, C); for(int i=0; i Matrix pow(Matrix a, long long int k) { assert(a.get_row() == a.get_col()); Matrix ret(a.get_row(), a.get_col(), true); for(; k>0; k>>=1) { if(k & 1) ret = ret * a; a = a * a; } return ret; } int main() { std::cin >> N >> MOD; Matrix P(2, 2); P[0] = {1, 1}; P[1] = {1, 0}; P = pow(P, N-2); Matrix ans(2, 1); ans[0][0] = 1; ans[1][0] = 0; ans = P * ans; std::cout << ans[0][0] << std::endl; return 0; }