#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
long long n,p,f[1005][150];
int main() {
n=read();p=read();
for(int i=0;i<=9;i++) f[1][1<<(i%7)]++;
for(int i=2;i<=n;i++)
for(int j=0;j<=127;j++) {
if(!f[i-1][j]) continue;
int pow=0;
for(int k=1;k<=64;k=(k<<1)) {
pow++;
if((j&k)==0) continue;
for(int t=0;t<=9;t++) {
f[i][((10*pow+t)%7)|j]=(f[i][((10*pow+t)%7)|j]+f[i-1][j])%p;
}
}
}
cout<<f[n][127]<<'\n';
}