#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll MM,MOD;
ll sum(ll A,ll B){
A=(A+MOD)%MOD;
B=(B+MOD)%MOD;
ll ans = (A+B)%MOD;
ans=(ans+MOD)%MOD;
return ans;
}
ll f(ll a,ll n){
ll res=a,ans=0;
while(n){
if(n%2) ans=(ans+res)%MOD;
res=(res+res)%MOD;
n/=2;
}
return ans;
}
struct matran{
ll a[3][3];
void print(){
for(ll i=0;i<3;i++){
for(ll j=0;j<3;j++) cout<<a[i][j]<<" ";
cout<<'\n';
}
}
} mot,M;
struct boba{
ll f_n2,f_n1,f_n;
void print(){
cout<<f_n2<<" "<<f_n1<<" "<<f_n<<'\n';
}
} init;
matran prod(matran A,matran B){
matran C;
for(ll i=0;i<3;i++)
for(ll j=0;j<3;j++) C.a[i][j]=0;
for(ll i=0;i<3;i++){
for(ll j=0;j<3;j++){
for(ll k=0;k<3;k++) C.a[i][j]=sum(C.a[i][j],f(A.a[i][k],B.a[k][j]));
}
}
return C;
}
matran po(matran A,ll n){
matran res=A,ans=mot;
while(n){
if(n%2) ans=prod(ans,res);
res=prod(res,res);
n/=2;
}
return ans;
}
boba prod1(boba p,matran A){
boba ans;
ans.f_n2=0;
ans.f_n1=1;
ans.f_n=0;
for(ll j=0;j<3;j++){
if(j==0){
ans.f_n2 = sum(sum(f(p.f_n2,A.a[0][j]),f(p.f_n1,A.a[1][j])),f(p.f_n,A.a[2][j]));
}
else if(j==1){
ans.f_n1 = sum(sum(f(p.f_n2,A.a[0][j]),f(p.f_n1,A.a[1][j])),f(p.f_n,A.a[2][j]));
}
else ans.f_n = sum(sum(f(p.f_n2,A.a[0][j]),f(p.f_n1,A.a[1][j])),f(p.f_n,A.a[2][j]));
}
return ans;
}
int main(){
ll tmp[3][3]={ {0,0,1},{1,0,1},{0,1,1}};
ll donvi[3][3]={{1,0,0},{0,1,0},{0,0,1}};
for(ll i=0;i<3;i++)
for(ll j=0;j<3;j++) {
M.a[i][j]=tmp[i][j];
mot.a[i][j]=donvi[i][j];
}
init.f_n2=1;
init.f_n1=2;
init.f_n=4;
// init.print();
ll n;
cin>>n>>MM;
MOD=MM;
boba ress = prod1(init,po(M,n-1));
cout<<ress.f_n2;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKbGwgTU0sTU9EOwpsbCBzdW0obGwgQSxsbCBCKXsKICAgICAgICBBPShBK01PRCklTU9EOwogICAgICAgIEI9KEIrTU9EKSVNT0Q7CiAgICAgICAgbGwgYW5zID0gKEErQiklTU9EOwogICAgICAgIGFucz0oYW5zK01PRCklTU9EOwogICAgICAgIHJldHVybiBhbnM7Cn0KbGwgZihsbCBhLGxsIG4pewogICAgICAgIGxsIHJlcz1hLGFucz0wOwogICAgICAgIHdoaWxlKG4pewogICAgICAgICAgICAgICAgaWYobiUyKSBhbnM9KGFucytyZXMpJU1PRDsKICAgICAgICAgICAgICAgIHJlcz0ocmVzK3JlcyklTU9EOwogICAgICAgICAgICAgICAgbi89MjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGFuczsKfQpzdHJ1Y3QgbWF0cmFuewogICAgICAgIGxsIGFbM11bM107CiAgICAgICAgdm9pZCBwcmludCgpewogICAgICAgICAgICAgICAgZm9yKGxsIGk9MDtpPDM7aSsrKXsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yKGxsIGo9MDtqPDM7aisrKSBjb3V0PDxhW2ldW2pdPDwiICI7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQ8PCdcbic7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgfQp9IG1vdCxNOwpzdHJ1Y3QgYm9iYXsKICAgICAgICBsbCBmX24yLGZfbjEsZl9uOwogICAgICAgIHZvaWQgcHJpbnQoKXsKICAgICAgICAgICAgICAgIGNvdXQ8PGZfbjI8PCIgIjw8Zl9uMTw8IiAiPDxmX248PCdcbic7CiAgICAgICAgfQp9IGluaXQ7Cm1hdHJhbiBwcm9kKG1hdHJhbiBBLG1hdHJhbiBCKXsKICAgICAgICBtYXRyYW4gQzsKICAgICAgICBmb3IobGwgaT0wO2k8MztpKyspCiAgICAgICAgICAgZm9yKGxsIGo9MDtqPDM7aisrKSBDLmFbaV1bal09MDsKICAgICAgICBmb3IobGwgaT0wO2k8MztpKyspewogICAgICAgICAgICAgICAgZm9yKGxsIGo9MDtqPDM7aisrKXsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yKGxsIGs9MDtrPDM7aysrKSBDLmFbaV1bal09c3VtKEMuYVtpXVtqXSxmKEEuYVtpXVtrXSxCLmFba11bal0pKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEM7Cn0KbWF0cmFuIHBvKG1hdHJhbiBBLGxsIG4pewogICAgICAgIG1hdHJhbiByZXM9QSxhbnM9bW90OwogICAgICAgIHdoaWxlKG4pewogICAgICAgICAgICAgICAgaWYobiUyKSBhbnM9cHJvZChhbnMscmVzKTsKICAgICAgICAgICAgICAgIHJlcz1wcm9kKHJlcyxyZXMpOwogICAgICAgICAgICAgICAgbi89MjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGFuczsKfQpib2JhIHByb2QxKGJvYmEgcCxtYXRyYW4gQSl7CiAgICAgICAgYm9iYSBhbnM7CiAgICAgICAgYW5zLmZfbjI9MDsKICAgICAgICBhbnMuZl9uMT0xOwogICAgICAgIGFucy5mX249MDsKICAgICAgICBmb3IobGwgaj0wO2o8MztqKyspewogICAgICAgICAgICAgICAgaWYoaj09MCl7CiAgICAgICAgICAgICAgICAgICAgICAgIGFucy5mX24yID0gc3VtKHN1bShmKHAuZl9uMixBLmFbMF1bal0pLGYocC5mX24xLEEuYVsxXVtqXSkpLGYocC5mX24sQS5hWzJdW2pdKSk7ICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlIGlmKGo9PTEpewogICAgICAgICAgICAgICAgICAgICAgICBhbnMuZl9uMSA9IHN1bShzdW0oZihwLmZfbjIsQS5hWzBdW2pdKSxmKHAuZl9uMSxBLmFbMV1bal0pKSxmKHAuZl9uLEEuYVsyXVtqXSkpOyAKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgYW5zLmZfbiA9IHN1bShzdW0oZihwLmZfbjIsQS5hWzBdW2pdKSxmKHAuZl9uMSxBLmFbMV1bal0pKSxmKHAuZl9uLEEuYVsyXVtqXSkpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gYW5zOwp9CmludCBtYWluKCl7CgogICAgIGxsIHRtcFszXVszXT17IHswLDAsMX0sezEsMCwxfSx7MCwxLDF9fTsKICAgICBsbCBkb252aVszXVszXT17ezEsMCwwfSx7MCwxLDB9LHswLDAsMX19OwogICAgIGZvcihsbCBpPTA7aTwzO2krKykKICAgICAgICBmb3IobGwgaj0wO2o8MztqKyspIHsKICAgICAgICAgICAgICAgIE0uYVtpXVtqXT10bXBbaV1bal07CiAgICAgICAgICAgICAgICBtb3QuYVtpXVtqXT1kb252aVtpXVtqXTsKICAgICAgICAgICAgICAgIH0KICAgICBpbml0LmZfbjI9MTsKICAgICBpbml0LmZfbjE9MjsKICAgICBpbml0LmZfbj00OwovLyAgICAgIGluaXQucHJpbnQoKTsKICAgICBsbCBuOwogICAgIGNpbj4+bj4+TU07CiAgICAgTU9EPU1NOwogICAgIGJvYmEgcmVzcyA9IHByb2QxKGluaXQscG8oTSxuLTEpKTsKICAgICBjb3V0PDxyZXNzLmZfbjI7CgogICAgIAp9