#include <cstdio>
#include <algorithm>
const int N = 6e3+5;
int n, P, f[N][N<<1], ans; void fix(int &x) { x<P?0:x-=P; }
int main()
{
    scanf("%d%d", &n, &P), n *= 3, f[0][N] = 1;
    for(int i = 0; i < n; ++i) for(int j = -i; j <= i; ++j)
        fix(f[i+1][j+1+N]+=f[i][j+N]), 
        fix(f[i+2][j-1+N]+=f[i][j+N]*(i+1ll)%P),
        fix(f[i+3][j+N]+=f[i][j+N]*(i+1ll)*(i+2ll)%P);
    for(int i = 0; i <= n; ++i) fix(ans+=f[n][i+N]); printf("%d\n", ans);
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0;i<(ll)n;++i)

set<ll> primeFactorization( ll n ){
  set<ll> a = {1};
  while( n != 1 ){
    ll root = (ll)sqrt(n);
    for(ll i=2;i<=n;++i){
	  if( i > root ){
        a.insert(n);
        n /= n;
        break;
      }
      if( n%i == 0 ){
        a.insert(i);
        n /= i;
        break;
      }
    }
  }
  return a;
}


int main() {
  ll a,b;cin >> a >> b;
  ll num = __gcd(a,b);
  set<ll> array = primeFactorization(num);

  //for( auto p : array ) cout << p << endl;
  cout << array.size() << endl;
  return 0;
}