#include<fstream>
#define FOR(a,b,c) for(register int a=b;a<=c;++a)
#define N 1<<20
#define P 20
#define SQ 350
using namespace std;
ifstream f("ratphu.in");
ofstream g("ratphu.out");
/*int dx[]={0,0,0,1,-1};
int dy[]={0,1,-1,0,0};*/
int cif[P],p,lim,t,i,j,r;
long long n,D[P][N];
int main ()
{
f>>n>>p;
t=-1;
while(n)
{
cif[++t]=n%10;
n/=10;
}
D[0][0]=1;
lim=1<<(t+1);
--lim;
if(p==1)
{
t++;
while(t)
{
D[0][0]*=t;
t--;
}
g<<D[0][0];
return 0;
}
for(i=0;i<=lim;++i)
for(r=0;r<p;++r)
if(D[r][i])
for(j=0;j<=t;++j)
if(!(i&(1<<j)))
D[(r*10+cif[j])%p][i|(1<<j)]+=D[r][i];
g<<D[0][lim];
return 0;
}
//Look at me! Look at me! The monster inside me has grown this big!