#include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define chmin(a, b) a = min(a, b) #define chmax(a, b) a = max(a, b) #define all(vec) vec.begin(), vec.end() using ll = long long; #define fill1(arr, num) fill(arr, arr + sizeof(arr) / sizeof(*arr), num); #define fill2(arr, num) fill(*arr, *arr + sizeof(arr) / sizeof(**arr), num); #define fill3(arr, num) fill(**arr, **arr + sizeof(arr) / sizeof(***arr), num); #define MOD 1000000007 vector> matmul(vector> mat1, vector> mat2, ll M) { vector> res(2, vector(2)); rep(i, 2) { rep(j, 2) { res[i][j] = (mat1[i][0] * mat2[0][j] + mat1[i][1] * mat2[1][j]) % M; } } return res; } int main() { ll N, M; cin >> N >> M; vector> mat = {{1, 1}, {1, 0}}, nmat = {{1, 0}, {0, 1}}; vector init_vec = {1, 1}; int cnt = 0; while ((1 << cnt) <= N) { if (N & (1 << cnt)) { nmat = matmul(nmat, mat, M); } mat = matmul(mat, mat, M); cnt += 1; } cout << nmat[1][1] << endl; }