#include <iostream>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
int n, m;
// {f[1], f[0], s[1]} * {{ 1, 1, 1 }, ^ (n+1) = {f[n+2], f[n+1], s[n+2]};
// { 1, 0, 0 },
// { 0, 0, 1 }}
// f[n+2] = f[n] + f[n-1];
// f[n] = f[n+2] - f[n-1];
// s[n] = f[n+2] - f[2];
//矩阵相乘 a * b = c
void mul(int a[][3], int b[][3], int c[][3])
{
int t[3][3] = {0};
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
for(int k = 0; k < 3; k++)
t[i][j] = (t[i][j] + a[i][k] * b[k][j]) % m;
memcpy(c, t, sizeof t);
}
signed main()
{
cin >> n >> m;
n++;
int res[3][3] = {1, 0, 1};
int a[3][3] = {{1, 1, 1}, {1, 0, 0}, {0, 0, 1}};
//快速幂求: res * (a ^ n)
while(n)
{
if(n & 1) mul(res, a, res);
mul(a, a, a);
n >>= 1;
}
cout << ((res[0][0] - 1) % m + m ) % m<< endl;
return 0;
}