#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ld INF = (ld)10000000000000;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;

ll dp[1001][2][4][4][4][4];
ll mod_pow(ll x, ll n, ll m) {
	ll ret = 1;
	while (n) {
		if (n % 2)ret = ret * x%m;
		x = x * x%m; n >>= 1;
	}
	return ret;
}
int main() {
	int n; ll m; cin >> n >> m;
	/*dp[0][0] = 1;
	rep1(i, n) {
		dp[i][0] = dp[i - 1][0] * 4 + dp[i - 1][1] * 3;
		dp[i][1] = dp[i - 1][1] * 4 + dp[i - 1][0] * 3;
		rep(j, 2) {
			dp[i][j] %= m;
		}
	}
	ll ans = dp[n][0] * 2 % m;
	rep1(i, n) {
		dp[i][0] = dp[i - 1][0] * 2 + dp[i - 1][1];
		dp[i][1] = dp[i - 1][1] * 2 + dp[i - 1][0] * 2;
		rep(j, 2) {
			dp[i][j] %= m;
		}
	}
	ans = ans - 8 * dp[n][0]; ans = (ans%m + m) % m;
	ll csum = 2 + mod_pow(2, n, m); csum *= 2;
	ans = (ans + csum) % m;
	cout << ans << endl;*/
	dp[0][0][3][2][1][3] = 1;
	/*dp[0][0][3][2][2][0] = 1;
	dp[0][0][3][2][0][1] = 1;
	dp[0][0][3][2][2][1] = 1;*/
	//dp[0][1][1][3][3][2] = 1;
	rep(i, n) {
		rep(j, 2) {
			rep(k, 4) {
				rep(l, 4) {
					rep(k1, 4) {
						rep(l1,4){
							rep(ul, 8) {
								int u = ul;
								if (j == 0 && u == 0)continue;
								if (j == 1 && u == 7)continue;
								int t[3];
								t[0] = u % 2; u /= 2;
								t[1] = u % 2; u /= 2;
								t[2] = u % 2;
								int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
								if (k % 2 && t[0])x1 = 1;
								if (k / 2 && t[2] == 0 && t[1] == 0)x1 = 1;
								if (l % 2 && t[0])y1 = 1;
								if (l / 2 && t[2] == 0 && t[1] == 0)y1 = 1;
								if (k % 2 && t[0] && t[1])x2 = 1;
								if (k / 2 && t[2] == 0)x2 = 1;
								if (l % 2 && t[0] && t[1])y2 = 1;
								if (l / 2 && t[2] == 0)y2 = 1;

								int x3 = 0, x4 = 0, y3 = 0, y4 = 0;
								if (k1 % 2 && t[0] == 0)x3 = 1;
								if (k1 / 2 && t[2] && t[1])x3 = 1;
								if (l1 % 2 && t[0] == 0)y3 = 1;
								if (l1 / 2 && t[2] && t[1])y3 = 1;
								if (k1 % 2 && t[0] == 0 && t[1] == 0)x4 = 1;
								if (k1 / 2 && t[2])x4 = 1;
								if (l1 % 2 && t[0] == 0 && t[1] == 0)y4 = 1;
								if (l1 / 2 && t[2])y4 = 1;


								(dp[i + 1][t[1]^1][x1 + 2 * x2][y1 + 2 * y2][x3+2*x4][y3+2*y4] += dp[i][j][k][l][k1][l1]) %= m;
							}

						}
					}
				}
			}
		}
	}
	ll ans = 0;
	rep(j, 4) {
		rep(k, 4) {
			rep(j1, 4) {
				rep(k1, 4) {
					if (j % 2)continue;
					if (k / 2)continue;
					if (j1%2)continue;
					if (k1/2)continue;
					ans += dp[n][0][j][k][j1][k1]; ans %= m;
					//ans += dp[n][1][j][k][j1][k1]; ans %= m;

				}
			}
		}
	}
	//cout << ans << endl;
	cout << 2*ans%m << endl;
	//stop
	return 0;
}




Measured memory_bytes: 7096