var l,ans,n,m,q,ost,q1:int64; i,j,t,p,x:longint; s:string; ok:boolean; a,qq:array [0..300000,0..101] of int64; aa,kol:array [0..200] of longint; begin read(q1,m); str(q1,s); l:=length(s); for i:=1 to l do begin val(s[i],aa[i],p); inc(kol[aa[i]]); end; for i:=1 to l do if aa[i]>0 then a[1 shl (i-1),aa[i] mod m]:=1; q:=(1 shl (l))-1; for i:=1 to q do begin for j:=1 to l do begin if ((1 shl (j-1)) and i=0) then begin ok:=true; for x:=1 to j-1 do if (aa[j]=aa[x]) and ((1 shl (x-1)) and i=0) then begin ok:=false; break; end; for x:=j+1 to l do if (aa[j]=aa[x]) and ((1 shl (x-1)) and i>0) then begin ok:=false; break; end; if not(ok) then continue; inc(qq[i,0]); qq[i,qq[i,0]]:=j; end; end; end; for i:=1 to q do begin for j:=0 to m-1 do begin if a[i,j]=0 then continue; for t:=1 to qq[i,0] do begin ost:=(j*10+aa[qq[i][t]]) mod m; inc(a[i+(1 shl (qq[i][t]-1)),ost],a[i][j]); end; end; end; writeln(a[q][0]); end.