#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

int n,m;

// first UD
// before UD
// 0-start right ongoing loop at up
// 0-start right ongoing loop at down
// 0-start left ongoing loop at up
// 0-start left ongoing loop at down
// 1-start right ongoing loop at up
// 1-start right ongoing loop at down
// 1-start left ongoing loop at up
// 1-start left ongoing loop at down
// 2^10 !?
ll dp[1252][1024];

int f(int a,int b,int c,int d,int e,int f,int g,int h,int i,int j){
  int ret = 0;
  ret |= a<<0;
  ret |= b<<1;
  ret |= c<<2;
  ret |= d<<3;
  ret |= e<<4;
  ret |= f<<5;
  ret |= g<<6;
  ret |= h<<7;
  ret |= i<<8;
  ret |= j<<9;
  return ret;
}

int main(){
  scanf("%d%d",&n,&m);
  dp[0][f(0,0, 1,1,1,0, 0,1,1,1)] = 1;
  dp[0][f(1,1, 1,0,1,1, 1,1,0,1)] = 1;
  int te = 0;
  REP(i,n)REP(state,1024){
    REP(ud,2)REP(uprl,2)REP(dorl,2){
      int first = state>>0&1;
      int before = state>>1&1;
      int usru = state>>2&1;
      int usrd = state>>3&1;
      int uslu = state>>4&1;
      int usld = state>>5&1;
      int dsru = state>>6&1;
      int dsrd = state>>7&1;
      int dslu = state>>8&1;
      int dsld = state>>9&1;
      if(i==n-1){
        if(first != ud)continue;
      }
      if(before && uprl && !ud && !dorl)continue;
      if(!before && !uprl && ud && dorl)continue;
      int nfirst = first;
      int nbefore = ud;
      int nusru = (usru && uprl) || (usrd && dorl && ud);
      int nusrd = (usru && uprl && !ud) || (usrd && dorl);
      int nuslu = (uslu && !uprl) || (usld && !ud && !dorl);
      int nusld = (uslu && ud && !uprl) || (usld && !dorl);
      int ndsru = (dsru && uprl) || (dsrd && ud && dorl);
      int ndsrd = (dsru && !ud && uprl) || (dsrd && dorl);
      int ndslu = (dslu && !uprl) || (dsld && !ud && !dorl);
      int ndsld = (dslu && ud && !uprl) || (dsld && !dorl);
      // if(i==n-1 && dp[i][state] > 0){
      //   printf("%d %d: %lld\n",i,state,dp[i][state]);
      //   te += dp[i][state];
      // }
      if(i==n-1){
        if(nusru || nuslu || ndsrd || ndsld)continue;
      }
      int nstate = f(nfirst,nbefore,nusru,nusrd,nuslu,nusld,ndsru,ndsrd,ndslu,ndsld);
      (dp[i+1][nstate] += dp[i][state]) %= m;
    }
  }
  // DEBUG(te);
  ll ans = 0;
  REP(state,1024){
    ans += dp[n][state];
    ans %= m;
  }
  printf("%lld\n",ans);
  return 0;
}

