#include using namespace std; typedef long long ll; class ModInt { using u64 = std::uint_fast64_t; static u64 &mod() { static u64 mod_ = 0; return mod_; } public: u64 x; ModInt(const ll x = 0) : x(x < 0 ? (mod() - (-x % mod())) % mod() : x % mod()) {} ModInt operator+(const ModInt r) { return ModInt(*this) += r; } ModInt operator*(const ModInt r) { return ModInt(*this) *= r; } ModInt operator-(const ModInt r) { return ModInt(*this) -= r; } ModInt operator/(const ModInt r) { return ModInt(*this) /= r; } ModInt operator-() { return ModInt(mod() - x); } ModInt &operator+=(const ModInt r) { x += r.x; if (x >= mod()) x -= mod(); return *this; } ModInt &operator-=(const ModInt r) { if (x < r.x) x += mod(); x -= r.x; return *this; } ModInt &operator*=(const ModInt r) { x *= r.x; if (x >= mod()) x %= mod(); return *this; } ModInt &operator/=(ModInt r) { if (!(x % r.x)) { x /= r.x; return *this; } u64 p = mod() - 2; while (p > 0) { if (p & 1) *this *= r; r *= r; p >>= 1; } return *this; } ModInt &operator++(int) { return (*this) += 1; } ModInt &operator++() { return (*this) += 1; } ModInt &operator--(int) { return (*this) -= 1; } ModInt &operator--() { return (*this) -= 1; } bool operator<(const ModInt r) { return x < r.x; } bool operator>(const ModInt r) { return x > r.x; } bool operator<=(const ModInt r) { return x <= r.x; } bool operator>=(const ModInt r) { return x >= r.x; } bool operator==(const ModInt r) { return x == r.x; } bool operator!=(const ModInt r) { return x != r.x; } ModInt inv() { return (ModInt)1 / (*this); } int get_mod() { return mod(); } friend std::istream &operator>>(std::istream &in, ModInt &m) { ll a; in >> a; if (a < 0) a = mod() - (-a % mod()); if (a >= mod()) a %= mod(); m.x = a; return in; } friend std::ostream &operator<<(std::ostream &out, const ModInt &m) { out << m.x; return out; } static void set_mod(const u64 m) { mod() = m; } }; using mint = ModInt; template class Matrix { vector> mat; public: const size_t height, width; Matrix(size_t height, size_t width, T e = 0) : height(height), width(width) { mat.assign(height, vector(width, e)); } Matrix(vector> m) : mat(m), height(m.size()), width(m[0].size()) {} Matrix operator+(const Matrix r) { return Matrix(*this) += r; } Matrix operator*(const Matrix r) { return Matrix(*this) *= r; } Matrix operator-(const Matrix r) { return Matrix(*this) -= r; } Matrix operator*(const T x) { return Matrix(*this) *= x; } Matrix operator-() { return Matrix(*this) *= -1; } Matrix &operator+=(const Matrix a) { assert(height == a.height && width == a.width); for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) mat[i][j] += a[i][j]; return *this; } Matrix &operator-=(const Matrix a) { assert(height == a.height && width == a.width); for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) mat[i][j] -= a[i][j]; return *this; } Matrix &operator*=(const Matrix a) { assert(width == a.height); vector> b(height, vector(a.width, 0)); for (int i = 0; i < height; i++) for (int j = 0; j < a.width; j++) for (int k = 0; k < width; k++) b[i][j] += mat[i][k] * a[k][j]; mat.swap(b); return *this; } Matrix &operator*=(const T x) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { mat[i][j] *= x; } } } T det() { assert(height == width); Matrix b(*this); T res = 1; for (int i = 0; i < width; i++) { int idx = -1; for (int j = i; j < width; j++) { if (b[i][j] != 0) idx = j; } if (idx == -1) return 0; if (i != idx) { res *= -1; swap(b[i], b[idx]); } res *= b[i][i]; T x = b[i][i]; for (int j = 0; j < width; j++) { b[i][j] /= x; } for (int j = i + 1; j < width; j++) { T a = b[j][i]; for (int k = 0; k < width; k++) { b[i][k] -= b[i][k] * a; } } } return res; } vector &operator[](int i) { return mat[i]; } const vector &operator[](int i) const { return mat[i]; } friend std::ostream &operator<<(std::ostream &out, const Matrix &a) { for (int i = 0; i < a.height; i++) { out << "( "; for (int j = 0; j < a.width; j++) out << a[i][j] << (j + 1 == a.width ? " )\n" : ","); } return out; } static Matrix I(size_t n) { Matrix I(n, n); for (int i = 0; i < n; i++) I[i][i] = 1; return I; } }; template T pow(T x, U n, T e = 1) { T res = e; while (n > 0) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } int main() { int n, m; cin >> n >> m; mint::set_mod(m); Matrix A({{0, 1}, {1, 1}}); vector> v = {{0}, {1}}; Matrix B(v); Matrix I = Matrix::I(2); cout << (pow, int>(A, n - 2, I) * B)[1][0] << endl; return 0; }