var
  m,i,j,k,t,p:longint;
  g,d:array[0..10] of longint;
  dp:array[0..100001, 0..101] of int64;
  n:int64;

begin
  readln(n,m);
  fillchar(d,sizeof(d),0);
  while n<>0 do
    begin
      inc(d[n mod 10]);n:=n div 10;
    end;
  g[0] := 1;
  for i:=1 to 10 do g[i]:=g[i-1]*(d[i-1]+1);
  fillchar(dp,sizeof(dp),0);dp[0,0]:=1;
  for i:=0 to g[10]-2 do
    begin
      if (i>=1) and (i<=d[0]) then continue;
      for j:=0 to m-1 do
        if dp[i,j]>0 then
          begin
            t:=i;
            for k:=0 to 9 do
              begin
                p:=t mod (d[k]+1);
                if p<d[k] then dp[i+g[k],(j*10+k) mod m]:=dp[i+g[k],(j*10+k) mod m]+dp[i,j];
                t:=t div (d[k]+1);
              end;
          end;
    end;
  writeln(dp[g[10]-1,0],'');
end.

var dp: array[0..300000, 0..99] of int64;
    d, cnt: array[1..18] of longint;
    m, i, j, k, len: longint;
    n, x, y, ans: int64;
begin
 read(n, m);
 x:= n;
 while x > 0 do
  begin
   inc(len);
   d[len]:= x mod 10;
   inc(cnt[ x mod 10 ]);
   x:= x div 10;
  end;
 for i:= 1 to (len shr 1) do
  begin
   j:= d[i];
   d[i]:= d[len - i + 1];
   d[len - i + 1]:= j;
  end;

 dp[0][0]:= 1;
 for i:= 0 to ((1 shl len) - 1) do
  for j:= 0 to m-1 do
   for k:= 1 to len do
    if (i and (1 shl (k - 1)) = 0) and (not ((i = 0) and (d[k] = 0))) then
     begin
      x:= i or (1 shl (k - 1));
      y:= (j * 10 + d[k]) mod m;
      dp[x][y]:= dp[x][y] + dp[i][j];
     end;

 ans:= dp[(1 shl len) - 1][0];
 for i:= 0 to 9 do
  for j:= 1 to cnt[i] do
   ans:= ans div j;

 write(ans);
end.

{$R-,Q-,O+}
type mass=array[0..1 shl 18,0..99] of int64;
     maxx=array[0..18] of int64;
     majj=array[0..9] of boolean;
var used:majj;s:string;a,r,b,x,n,md,max1,res,c,k:int64;m:mass;m1,m2:maxx;w,i,j,z:longint;
begin
//  assign(input,'input.txt');
//  assign(output,'output.txt');
  readln(n,md);
  str(n,s);
  n:=length(s);
  for i:=1 to length(s) do
      m1[i]:=ord(s[i])-ord('0');
  m2[0]:=1;
  for i:=1 to length(s) do
     m2[i]:=m2[i-1]*10;
  for i:=1 to 1 shl n-1 do
     Begin
         b:=0;
         for z:=0 to n-1 do
            if (i and (1 shl z)>0)
            then inc(b);
         if b=1
         then Begin
                 for z:=0 to n-1 do
                    if (i and (1 shl z)>0)
                    then Begin
                            m[i,m1[z+1] mod md]:=1;
                            break;
                         end;
                 continue;
              end;
         fillchar(used,sizeof(used),true);
         for z:=0 to n-1 do
            if ((i shr z) and 1>0) and (used[m1[z+1]])
            then Begin
                    used[m1[z+1]]:=false;
                    a:=i xor (1 shl z);
                    if (b=n) and (m1[z+1]=0)
                    then continue;
                    c:=m2[b-1]*m1[z+1];
                    for w:=0 to md-1 do
                       inc(m[i,(c+w) mod md],m[a,w]);
                 end;
     end;
  writeln(m[1 shl n-1,0]);
end.

{$R-,Q-,O+}
type mass=array[0..1 shl 18,0..99] of int64;
     maxx=array[0..18] of int64;
     majj=array[0..9] of boolean;
     mahh=array[0..18,0..18] of int64;
var used:majj;s:string;a,r,b,x,n,md,max1,res,c,k:int64;m:mass;m2:mahh;m1:maxx;w,i,j,z:longint;
begin
//  assign(input,'input.txt');
//  assign(output,'output.txt');
  readln(n,md);
  str(n,s);{}
  n:=length(s);
  for i:=1 to length(s) do
    Begin
      m1[i]:=ord(s[i])-ord('0');
      m2[i,0]:=1;
      for z:=1 to length(s) do
         m2[i,z]:=m2[i,z-1]*10;
      for z:=0 to length(s) do
         m2[i,z]:=m2[i,z]*m1[i];
    end;
  for i:=1 to 1 shl n-1 do
     Begin
         b:=0;
         for z:=0 to n-1 do
            if (i and (1 shl z)>0)
            then inc(b);
         if b=1
         then Begin
                 for z:=0 to n-1 do
                    if (i and (1 shl z)>0)
                    then Begin
                            m[i,m1[z+1] mod md]:=1;
                            break;
                         end;
                 continue;
              end;
         fillchar(used,sizeof(used),true);
         for z:=0 to n-1 do
            if ((i shr z) and 1>0) and (used[m1[z+1]])
            then Begin
                    used[m1[z+1]]:=false;
                    a:=i xor (1 shl z);
                    if (b=n) and (m1[z+1]=0)
                    then continue;
                    for w:=0 to md-1 do
                       inc(m[i,(m2[z+1,b-1]+w) mod md],m[a,w]);
                 end;
     end;
  writeln(m[1 shl n-1,0]);{}
end.

{$R-,Q-,O+}
type mass=array[0..1 shl 18,0..99] of int64;
     maxx=array[0..18] of int64;
     majj=array[0..9] of boolean;
     mahh=array[0..18,0..18,-1..99] of int64;
var used:majj;s:string;a,r,b,x,n,md,max1,res,c,k:int64;m:mass;m2:mahh;m1:maxx;w,i,j,z:longint;
begin
//  assign(input,'input.txt');
//  assign(output,'output.txt');
  readln(n,md);
  str(n,s);
  n:=length(s);
  for i:=1 to length(s) do
    Begin
      m1[i]:=ord(s[i])-ord('0');
      m2[i,0,-1]:=1;
      for z:=1 to length(s) do
         m2[i,z,-1]:=m2[i,z-1,-1]*10;
      for z:=0 to length(s) do
        Begin
         m2[i,z,-1]:=m2[i,z,-1]*m1[i];
         for j:=0 to 99 do
            m2[i,z,j]:=(m2[i,z,-1]+j) mod md;
        end;
    end;
  for i:=1 to 1 shl n-1 do
     Begin
         b:=0;
         for z:=0 to n-1 do
            if (i and (1 shl z)>0)
            then inc(b);
         if b=1
         then Begin
                 for z:=0 to n-1 do
                    if (i and (1 shl z)>0)
                    then Begin
                            m[i,m1[z+1] mod md]:=1;
                            break;
                         end;
                 continue;
              end;
         fillchar(used,sizeof(used),true);
         for z:=0 to n-1 do
            if ((i shr z) and 1>0) and (used[m1[z+1]])
            then Begin
                    used[m1[z+1]]:=false;
                    a:=i xor (1 shl z);
                    if (b=n) and (m1[z+1]=0)
                    then continue;
                    for w:=0 to md-1 do
                       inc(m[i,m2[z+1,b-1,w]],m[a,w]);
                 end;
     end;
  writeln(m[1 shl n-1,0]);{}
end.

{$R-,Q-,O+}
type mass=array[0..1 shl 18,0..99] of int64;
     maxx=array[0..18] of int64;
     majj=array[0..9] of boolean;
var used:majj;s:string;a,r,b,x,n,md,max1,res,c,k:int64;m:mass;m1,m2:maxx;w,i,j,z:longint;
begin
//  assign(input,'input.txt');
//  assign(output,'output.txt');
  readln(n,md);
  str(n,s);
  n:=length(s);
  for i:=1 to length(s) do
      m1[i]:=ord(s[i])-ord('0');
  m2[0]:=1;
  for i:=1 to length(s) do
     m2[i]:=m2[i-1]*10;
  for i:=1 to 1 shl n-1 do
     Begin
         b:=0;
         for z:=0 to n-1 do
            if (i and (1 shl z)>0)
            then inc(b);
         if b=1
         then Begin
                 for z:=0 to n-1 do
                    if (i and (1 shl z)>0)
                    then Begin
                            m[i,m1[z+1] mod md]:=1;
                            break;
                         end;
                 continue;
              end;
         fillchar(used,sizeof(used),true);
         for z:=0 to n-1 do
            if ((i shr z) and 1>0) and (used[m1[z+1]])
            then Begin
                    used[m1[z+1]]:=false;
                    a:=i xor (1 shl z);
                    if (b=n) and (m1[z+1]=0)
                    then continue;
                    c:=m2[b-1]*m1[z+1];
                    for w:=0 to md-1 do
                       inc(m[i,(c+w) mod md],m[a,w]);
                 end;
     end;
  writeln(m[1 shl n-1,0]);{}
end.

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.

var dp:array[0..1 shl 18,0..100] of int64;
	next,first:array[0..20] of longint;
	n:int64;
	m,i,j,k,l,cnt,t:longint;
begin
	//assign(input,'i.in');reset(input);
	readln(n,m);
	while n > 0 do
	begin
		inc(cnt);
		t := n mod 10;
		next[cnt] := first[t];
		first[t] := cnt;
		n := n div 10;
	end;
	fillchar(dp,sizeof(dp),0);
	dp[0,0] := 1;
	for i := 0 to 1 shl cnt - 1 do
	for j := 0 to m -1 do
	begin
		if dp[i,j] = 0 then continue;
		for k := 0 to 9 do
		begin
			l := first[k];
			while l > 0 do
			begin
				if (i and (1 shl (l-1)) > 0) then l := next[l] else break;
			end;
			if (l > 0) and not ((i = 0) and (k = 0)) then inc(dp[i or (1 shl (l - 1)),(j * 10 + k) mod m], dp[i,j]);
		end;
	end;
	writeln(dp[1 shl cnt - 1, 0]);
	{for i := 0 to 7 do
	begin
	for j := 0 to 1 do write(dp[i,j],' ');
	writeln;
	end; }
	close(input);
end.

var k,n,m,x,y,z:int64;i,j,l:longint;
    a:array[0..400000,0..100]of int64;
    p,f:array[0..25]of longint;
 begin
 //  assign(input,'input.txt');reset(input);
 // assign(output,'output.txt');rewrite(output);
    readln(n,m);
    k:=0;
    while n>0 do
     begin
      inc(k);p[k]:=n mod 10;
      n:=n div 10;
     end;
    z:=(1 shl k)-1;
    a[0,0]:=1;x:=0;
    for i:=0 to z do
     for j:=0 to m-1 do
      begin
       if a[i,j]=0 then continue;
       inc(x);
       for l:=1 to k do
        begin
         y:=1 shl (l-1);
         if(f[p[l]]<>x)and(i and y=0)and((i<>0)or(p[l]<>0))then
          begin
           f[p[l]]:=x;
           inc(a[i or y,(j*10+p[l])mod m],a[i,j]);
          end;
        end;
      end;
    writeln(a[z,0]);

 end.

{$r-,s-,q-,i-,o+}
var
  n,m:int64;
  g,i,j,ost,l,vzyali:longint;s:ansistring;
  code : integer;
  c : char;
  f : array [0..1 shl 19,0..100] of int64;

  function set1(m,b:int64):int64;begin exit(m or (1 shl ((length(s)-b+1)-1)));end;
  function get(m,b:int64):int64;begin exit(ord((m and (1 shl ((length(s)-b+1)-1))=(1 shl ((length(s)-b+1)-1)))));end;
  function newost(x,y:int64):int64;var res:int64;begin res:=x;res:=(10*res)mod m;res:=(y+res)mod m;exit(res mod m); end;
  function cifra(c:char):longint;var res:longint;code:integer;begin val(c,res,code);exit(res);end;

begin
  readln(n,m);str(n,s);
  g:=length(s);
  fillchar(f,sizeof(f),0);
  f[0,0]:=1;
  for i:=0 to (1 shl g)-1 do
    for ost:=0 to m-1 do
    if f[i,ost]>0 then
    begin
     for c:='0' to '9' do
        begin
          if(c='0')and(i=0)then continue;
          val(c,j,code);
          for l:=1 to g do
            if (s[l]=c) and (get(i,l)=0) then
              begin
                f[set1(i,l),newost(ost,j)]:=f[set1(i,l),newost(ost,j)]+f[i,ost];
                break;
              end;
        end;
    end;
  writeln(f[(1 shl g)-1,0]);
end.

const fi=''; fo='';

Var n,m:longint;
    last:longword;
    d:array[0..262150,0..110] of Qword;
    pow:array[0..20] of Qword;
    list:array[0..20] of byte;
    A,B:array[0..20] of byte;
    dd:array[0..10] of boolean;
    vt:array[0..10] of Longint;

Procedure doc;
Var kk:Qword; i:longint;
begin
    assign(input,fi); reset(input);
    Readln(KK,m);
    while (kk<>0) do
    begin
        inc(n);
        B[n]:=kk mod 10;
        kk:=kk div 10;
    end;
    for i:=1 to n do
        A[i]:=B[n-i+1];
end;

Function getbit(x:longword; i:byte):byte;
begin
    getbit:=(x Shr i) and 1;
end;

Function bitoff(x:longword; i:byte):longword;
begin
    bitoff:=x and (not (1 Shl i));
end;

procedure xuly;
Var s,p:longword;
    i,j,k,kk,k1:longint;
    x:Qword;
begin
    pow[1]:=1;
    for i:=2 to n do
        pow[i]:=pow[i-1]*10;
    last:=(1 Shl n)-1;
    for s:=1 to last do
        begin
            for i:=0 to 9 do
                dd[i]:=false;
            k:=0;
             for i:=0 to n-1 do
                 if getbit(s,i)=1 then
                  begin
                     inc(k);
                     dd[a[i+1]]:=true;
                     vt[a[i+1]]:=i;
                     end;
             for i:=0 to 9 do
              if dd[i] then
               If (k<>n) or (i<>0) then
                 begin
                     j:=vt[i];
                     p:=bitoff(s,j);
                     x:=(pow[k]*i) mod m;
                     for kk:=0 to m-1 do
                         begin
                             k1:=(x+kk) mod m;
                             d[s,k1]:=d[s,k1]+d[p,kk];
                         end;
                 end;
        end;
end;

procedure ghi;
begin
     assign(output,fo); rewrite(output);
     writeln(d[last,0]);
end;

begin
    doc;
    D[0,0]:=1;
    xuly;
    ghi;
end.

var l,ans,n,m,q,ost,q1:int64;
  i,j,t,p,x:longint;
  s:string;
  ok:boolean;
    dp,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 dp[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 dp[i,j]=0 then continue;
             for t:=1 to qq[i,0] do
              begin
                ost:=(j*10+aa[qq[i][t]]) mod m;
                inc(dp[i+(1 shl (qq[i][t]-1)),ost],dp[i][j]);
                end;
           end;
      end;
       writeln(dp[q][0]);
   end.

var
    m,i,j,k,t,p:longint;
    g,d:array[0..10] of longint;
    dp:array[0..100010,0..101] of int64;
    n:int64;

begin {main}
  readln(n,m);
  fillchar(d,sizeof(d),0);
  while n<>0 do
    begin
      inc(d[n mod 10]);n:=n div 10;
    end;
  g[0]:=1;
  for i:=1 to 10 do g[i]:=g[i-1]*(d[i-1]+1);
  fillchar(dp,sizeof(dp),0);dp[0,0]:=1;
  for i:=0 to g[10]-2 do
    begin
      if (i>=1) and (i<=d[0]) then continue;
      for j:=0 to m-1 do
        if dp[i,j]>0 then
          begin
            t:=i;
            for k:=0 to 9 do
              begin
                p:=t mod (d[k]+1);
                if p<d[k] then dp[i+g[k],(j*10+k) mod m]:=dp[i+g[k],(j*10+k) mod m]+dp[i,j];
                t:=t div (d[k]+1);
              end;
          end;
    end;
  writeln(dp[g[10]-1,0]);
end.

var
    m,i,j,k,t,p:longint;
    g,d:array[0..10] of longint;
    dp:array[0..100010,0..101] of int64;
    n:int64;

begin {main}
  readln(n,m);
  fillchar(d,sizeof(d),0);
  while n<>0 do
    begin
      inc(d[n mod 10]);n:=n div 10;
    end;
  g[0]:=1;
  for i:=1 to 10 do g[i]:=g[i-1]*(d[i-1]+1);
  fillchar(dp,sizeof(dp),0);dp[0,0]:=1;
  for i:=0 to g[10]-2 do
    begin
      if (i>=1) and (i<=d[0]) then continue;
      for j:=0 to m-1 do
        if dp[i,j]>0 then
          begin
            t:=i;
            for k:=0 to 9 do
              begin
                p:=t mod (d[k]+1);
                if p<d[k] then dp[i+g[k],(j*10+k) mod m]:=dp[i+g[k],(j*10+k) mod m]+dp[i,j];
                t:=t div (d[k]+1);
              end;
          end;
    end;
  writeln(dp[g[10]-1,0],'');
end.

CONST
        tfi     ='';//'d.inp';
        tfo     ='';//'d.out';
        nmax    =100000;
TYPE
        arr     =array [0..1 shl 18,0..100] of int64;
        arr1    =array [0..20] of longint;
VAR
        fi,fo   :text;
        k       :longint;
        f       :arr;
        a       :string;
        n,res   :int64;
        c,b     :arr1;
 (*********************************************************************)
Procedure lam;
      Var
        i,j,t,tt,l,p    :longint;

      Begin
        read(fi,n,k);
        str(n,a);
        l:=length(a);
        for i:=1 to l do
          begin
            val(a[i],t);
            inc(c[t]);
            b[i]:=t;
          end;
        f[0,0]:=1;
        for i:=0 to (1 shl l)-1 do
          for j:=0 to k-1 do
            if f[i,j]<>0 then
              begin
                for t:=1 to l do
                  if (i and (1 shl (t-1))=0) and not((a[t]='0') and (i=0))  then
                    begin
                      tt:=i or (1 shl (t-1));
                      inc(f[tt,(j*10+b[t]) mod k],f[i,j]);
                    end;
              end;
        res:=f[(1 shl l)-1,0];
        for i:=0 to 9 do
          if c[i]<>0 then
            for j:=1 to c[i] do res:=res div j;
        write(fo,res);
      End;
 (**********************************************************************)
BEGIN
      assign(fi,tfi);reset(fi);
      assign(fo,tfo);rewrite(fo);
        lam;
      close(fi);
      close(fo);
ENd.

{$R-,Q-,O+}
type mass=array[0..1 shl 18,0..99] of int64;
     maxx=array[0..18] of int64;
     majj=array[0..9] of boolean;
     mahh=array[0..18,0..18,-1..99] of int64;
var used:majj;s:string;a,r,b,x,n,md,max1,res,c,k:int64;m:mass;m2:mahh;m1:maxx;w,i,j,z:longint;
begin
//  assign(input,'input.txt');
//  assign(output,'output.txt');
  readln(n,md);
  str(n,s);{}
  n:=length(s);
  for i:=1 to length(s) do
    Begin
      m1[i]:=ord(s[i])-ord('0');
      m2[i,0,-1]:=1;
      for z:=1 to length(s) do
         m2[i,z,-1]:=m2[i,z-1,-1]*10;
      for z:=0 to length(s) do
        Begin
         m2[i,z,-1]:=m2[i,z,-1]*m1[i];
         for j:=0 to 99 do
            m2[i,z,j]:=(m2[i,z,-1]+j) mod md;
        end;
    end;
  for i:=1 to 1 shl n-1 do
     Begin
         b:=0;
         for z:=0 to n-1 do
            if (i and (1 shl z)>0)
            then inc(b);
         if b=1
         then Begin
                 for z:=0 to n-1 do
                    if (i and (1 shl z)>0)
                    then Begin
                            m[i,m1[z+1] mod md]:=1;
                            break;
                         end;
                 continue;
              end;
         fillchar(used,sizeof(used),true);
         for z:=0 to n-1 do
            if ((i shr z) and 1>0) and (used[m1[z+1]])
            then Begin
                    used[m1[z+1]]:=false;
                    a:=i xor (1 shl z);
                    if (b=n) and (m1[z+1]=0)
                    then continue;
                    for w:=0 to md-1 do
                       inc(m[i,m2[z+1,b-1,w]],m[a,w]);
                 end;
     end;
  writeln(m[1 shl n-1,0]);{}
end.

var
  m,i,j,k,t,p:longint;
  g,d:array[0..10] of longint;
  dp:array[0..100001, 0..101] of int64;
  n:int64;

begin
  readln(n,m);
  fillchar(d,sizeof(d),0);
  while n<>0 do
    begin
      inc(d[n mod 10]);n:=n div 10;
    end;
  g[0]:=1;
  for i:=1 to 10 do g[i]:=g[i-1]*(d[i-1]+1);
  fillchar(dp,sizeof(dp),0);dp[0,0]:=1;
  for i:=0 to g[10]-2 do
    begin
      if (i>=1) and (i<=d[0]) then continue;
      for j:=0 to m-1 do
        if dp[i,j]>0 then
          begin
            t:=i;
            for k:=0 to 9 do
              begin
                p:=t mod (d[k]+1);
                if p<d[k] then dp[i+g[k],(j*10+k) mod m]:=dp[i+g[k],(j*10+k) mod m]+dp[i,j];
                t:=t div (d[k]+1);
              end;
          end;
    end;
  writeln(dp[g[10]-1,0],'');
end.

{$R-,S-,Q-,I-,O+}
{$M 32000000}

{$APPTYPE CONSOLE}

uses Math;
const
    inf = round(1e9);
    maxn = round(1e5) + 200;
    md = round(1e9) + 7;
type
    pair = record
             mask, rem : longint;
           end;
var
    n : int64;
    m, mask, rem, l, r, len, i, tmp, newmask, newrem : longint;
    used : array [0..9] of boolean;
    dp : array [0..1 shl 18 - 1, 0..100] of int64;
    c : array [0..18] of longint;
    cnt : array [0..1 shl 18 - 1] of longint;
    q : array [1..1 shl 25] of pair;
    pow : longint;

  function cntbits(x : longint) : longint;
  var
      res : longint;
  begin
    res := 0;
    while x > 0 do
    begin
      inc(res);
      x := x and (x - 1);
    end;
    cntbits := res;
  end;

begin
  //assign (input, 'input.txt'); reset(input);
  //assign (output, 'output.txt'); rewrite(output);
  randomize;
  readln(n, m);
  len := 0;
  while n > 0 do
  begin
    inc(len);
    c[len] := n mod 10;
    n := n div 10;
  end;
  for i := 1 to len shr 1 do
  begin
    tmp := c[i]; c[i] := c[len - i + 1]; c[len - i + 1] := tmp;
  end;
  for mask := 0 to 1 shl len - 1 do cnt[mask] := cntbits(mask);
  pow := 10 mod m;

  fillchar(dp, sizeof(dp), -1);
  l := 0;
  r := 0;
  for i := 1 to len do
  if not used[c[i]] and (c[i] > 0) then
  begin
    inc(r);
    q[r].mask := 1 shl (i - 1);
    q[r].rem := c[i] mod m;
    dp[q[r].mask][q[r].rem] := 1;
    used[c[i]] := true;
  end;

  while l < r do
  begin
    inc(l);
    mask := q[l].mask;
    rem := q[l].rem;
    fillchar(used, sizeof(used), false);
    for i := 1 to len do
    if (mask and (1 shl (i - 1)) = 0) and not used[c[i]] then
    begin
      used[c[i]] := true;
      newmask := mask or (1 shl (i - 1));
      newrem := (rem * pow + c[i]) mod m;
      if dp[newmask][newrem] = -1 then
      begin
        dp[newmask][newrem] := dp[mask][rem];
        inc(r);
        q[r].mask := newmask;
        q[r].rem := newrem;
      end
      else
        dp[newmask][newrem] := dp[newmask][newrem] + dp[mask][rem];
    end;
  end;

  writeln(max(0, dp[1 shl len - 1][0]));
end.

const
  b=18;
  l=1 shl b;
var
  a:array[0..b] of longint;
  f:array[0..l,0..99] of int64;
  good:array[0..1000] of longint;
  lim,mask,ost,used,m,k,i,nn,nmask,j:longint;
  prev:integer;
  n:int64;

begin
  readln(n,m);
  for i:=0 to 1000 do good[i]:=i mod m;
  k:=0;
  while (n<>0) do
  begin
    a[k]:=n mod 10;
    inc(k);
    n:=n div 10;
  end;
  for i:=0 to k-1 do for j:=0 to k-1 do if (a[i]>a[j]) then begin ost:=a[i]; a[i]:=a[j]; a[j]:=ost; end;
  lim:=(1 shl k)-1;
  f[0,0]:=1;
  for mask:=0 to lim-1 do for ost:=0 to m-1 do
  begin
    if (f[mask,ost]=0) then continue;
    prev:=-1;
    for i:=0 to k-1 do
    begin
      if ((mask and (1 shl i))<>0) then continue;
      if (mask=0) and (a[i]=0) then continue;
      if (prev=a[i]) then continue;
      nmask:=mask xor (1 shl i);
      nn:=good[ost*10+a[i]];
      f[nmask,nn]:=f[nmask,nn]+f[mask,ost];
      prev:=a[i];
    end;
  end;
  writeln(f[lim,0]);
end.

var dp:array[0..(1 shl 18),0..101] of int64;
    s:string;
    m,len,last_mask,i,ost,mask,j:longint;
    n:int64;
    a,b:array[0..100]of longint;
begin

 read(n,m);
  str(n,s);
  len:=length(s);
   last_mask:=1<<len;
   dec(last_mask);

    for i:=1 to len do
     a[i-1]:=ord(s[i])- 48;

     for i:=1 to len do inc(b[a[i-1]]);

      for i:=0 to len-1 do
       if a[i]>0 then inc(dp[1<<i][a[i] mod m]);

    for mask:=0 to last_mask do
       for i:=0 to len-1 do

         if mask and (1<<i) = 0 then
          for ost:=0 to m-1 do
           inc(dp[mask or (1<<i)][(ost*10+a[i]) mod m],dp[mask][ost]);

   for i:=0 to 9 do
    for j:=2 to b[i] do dp[last_mask][0]:=dp[last_mask][0] div j;

    writeln(dp[last_mask][0]);

end.

const
    fi='';//'401D.inp';
    mx=1 shl 18;
var
    f               :Text;
    n,m             :Longint;
    d               :Array[0..mx,0..100] of Int64;
    a,cnt,p         :Array[0..20] of Longint;
    s,res           :Int64;

procedure Doc;
var
    i       :Longint;
    x       :String;
begin
    read(f,s,m);
    Str(s,x);
    N:=length(x);
    for i:=1 to N do a[i]:=ord(x[i]) - ord('0');
    p[0]:=1;
    for i:=1 to 18 do p[i]:=p[i-1]*2;
end;

procedure Cal;
var
    i,j,k,u,v,g       :Longint;
begin
    d[0,0]:=1;   g:=1 shl n-2;
    for i:=0 to G do
        for j:=0 to M-1 do
            if d[i,j] >0 then
    begin
        for k:=1 to n do
            if (i and p[k-1] =0) and ((i >0) or ((i =0) and (a[k] >0))) then
        begin
            u:=i + p[k-1];
            v:=(j*10 + a[k]) mod m;
            d[u,v]:=d[u,v] + d[i,j];
        end;
    end;
end;

procedure Inkq;
var
    i,j     :Longint;
begin
    res:=d[p[N] - 1,0];
    for i:=0 to 9 do cnt[i]:=0;
    for i:=1 to N do inc(cnt[a[i]]);

    for i:=0 to 9 do
        for j:=2 to cnt[i] do res:=res div j;
    writeln(res);
end;

begin
    assign(f,fi);reset(f);
        Doc;
        Cal;
        Inkq;
    close(f);
end.

var
  n : int64;
  i, j, ii, m, d, count : integer;
  st : string;
  a, bb : array[1..18] of integer;
  z : array[1..19] of int64;
  c : array[1..270000,0..99] of int64;
  y : array[1..50000] of int64;

procedure generate(t : integer);
var b : array[1..17] of byte;
    i, j : integer;
    p, x, k, kk : int64;
begin
        fillchar(b,sizeof(b),0);
        for j:=1 to t do b[j]:=j-1;
        while true do
        begin
                k:=0;
                for j:=1 to t do k:=k+(1 shl b[j]);
                inc(count); y[count]:=k;
                for j:=1 to t do
                begin
                  kk:=1 shl b[j]; x:=z[b[j]+1]; p:=(a[t]*x) mod m;
                  if (a[t]=0)and(b[j]=d-1) then continue;
                  for i:=0 to m-1 do
                    if i+p<m then c[k,i+p]:=c[k,i+p]+c[k-kk,i]
                             else c[k,i+p-m]:=c[k,i+p-m]+c[k-kk,i];
                end;
                p:=t;
                while (b[p]=d-t+p-1) and (p>0) do dec(p);
                if p=0 then break;
                inc(b[p]);
                for j:=p+1 to t do b[j]:=b[j-1]+1;
        end;
end;

begin
  read(n,m);
  str(n,st); d:=length(st);
  for i:=1 to d do a[i]:=ord(st[i])-48;
  z[1]:=1;
  for i:=2 to d do z[i]:=z[i-1]*10;
  fillchar(c,sizeof(c),0);
  for i:=1 to d do c[1 shl (i-1),(a[1]*z[i]) mod m]:=1;

  fillchar(bb,sizeof(bb),0);
  for i:=2 to d do
  for j:=i-1 downto 1 do
    if a[i]=a[j] then inc(bb[i]);

  for i:=2 to d do
  begin
    count:=0;
    generate(i);
    for ii:=1 to count do
    for j:=0 to m-1 do
      c[y[ii],j]:=c[y[ii],j] div (bb[i]+1);
  end;
  writeln(c[(1 shl d)-1,0]);

end.

const maxcs=19;
      maxm=100;

var f:array[0..262143,0..maxm,false..true] of int64;

    a,lt:array[0..maxcs] of longint;

    d:array[0..9] of longint;

    n,m:longint;

    res:int64;

procedure doc;
var i:longint;
    st:string;
    tg:int64;
begin
    readln(tg,m);
    str(tg,st);

    n:=length(st);

    for i:=1 to n do
        begin
            a[i]:=ord(st[i])-48;
            inc(d[a[i]]);
        end;

    lt[0]:=1;
    for i:=1 to n do lt[i]:=lt[i-1]*2;
end;

procedure xuly;
var i,j,k,newi,newj:longint;
    start,newstart:boolean;
begin
    f[0,0,false]:=1;

    for i:=0 to lt[n]-1 do
        for j:=0 to m do
            for start:=false to true do
                if f[i,j,start]<>0 then
                    begin

                        for k:=1 to n do
                            if (i and lt[k-1]=0) and ((a[k]<>0) or ((a[k]=0) and start)) then
                                begin
                                    newi:=i or lt[k-1];
                                    newj:=(j*10+a[k]) mod m;
                                    newstart:=start or (a[k]<>0);

                                    f[newi,newj,newstart]:=f[newi,newj,newstart]+f[i,j,start];
                                end;
                    end;

    res:=f[lt[n]-1,0,true];

    for i:=0 to 9 do
        for j:=2 to d[i] do
            res:=res div j;

    writeln(res);
end;

begin

    //assign(input,'nhap.inp'); reset(input);

    doc;
    xuly;
end.

{$mode objFpc}
uses math;
const
                finp            =               '';
                fout            =               '';
                maxcs           =               18;
type
                cs              =               array[0..20] of integer;
var
                fi,fo           :               text;
                n               :               int64;
                m,sl,vc         :               integer;
                x               :               cs;
                fx              :               array[0..262500,0..110] of int64;
                gt              :               array[0..20] of int64;
                dd              :               array[0..9] of integer;
procedure openfile;
begin
        assign(fi,finp);reset(fi);
        assign(fo,fout);rewrite(fo);
end;
procedure closefile;
begin
        close(fi);close(fo);
end;
procedure enter;
var
        i               :               integer;
begin
        readln(fi,n,m);
end;
procedure swap(t:int64);
begin
        sl:=0;
        repeat
                inc(sl);
                x[sl]:=t mod 10;
                t:=t div 10;
        until t=0;
end;
function get(t,x:integer):integer;
begin
        result:=(t>>(x-1)) and 1;
end;
procedure process;
var
        i,j,t,k             :               integer;
        r                   :               int64;
begin
        swap(n);
        gt[0]:=1;
        for i:=1 to 18 do
                gt[i]:=gt[i-1]*i;
        fx[0,0]:=1;
        vc:=(1<<sl) -1;
        for i:=0 to vc do
        for k:=0 to m do
        begin
                for j:=1 to sl do
                begin
                        if (x[j]=0) and (i=0) then continue;
                        if get(i,j)=0 then
                        begin
                                t:=i or (1<<(j-1));
                                inc(fx[t,(k*10+x[j]) mod m],fx[i,k]);
                        end;
                end;
        end;
        r:=fx[vc,0];
        for i:=1 to sl do
                inc(dd[x[i]]);
        for i:=0 to 9 do
                r:=r div gt[dd[i]];
        writeln(fo,r);
end;
begin
        openfile;
        enter;
        process;
        closefile;
end.

{$R-,Q-,O+}
type mass=array[0..1 shl 18,0..99] of longint;
     maxx=array[0..18] of int64;
     majj=array[0..9] of boolean;
var used:majj;s:string;a,r,b,x,n,md,max1,res,c,k:int64;m:mass;m1,m2:maxx;w,i,j,z:longint;
begin
//  assign(input,'input.txt');
//  assign(output,'output.txt');
  readln(n,md);
  str(n,s);
  n:=length(s);
  for i:=1 to length(s) do
      m1[i]:=ord(s[i])-ord('0');
  m2[0]:=1;
  for i:=1 to length(s) do
     m2[i]:=m2[i-1]*10;
  for i:=1 to 1 shl n-1 do
     Begin
         b:=0;
         for z:=0 to n-1 do
            if (i and (1 shl z)>0)
            then inc(b);
         if b=1
         then Begin
                 for z:=0 to n-1 do
                    if (i and (1 shl z)>0)
                    then Begin
                            m[i,m1[z+1] mod md]:=1;
                            break;
                         end;
                 continue;
              end;
         fillchar(used,sizeof(used),true);
         for z:=0 to n-1 do
            if ((i shr z) and 1>0) and (used[m1[z+1]])
            then Begin
                    used[m1[z+1]]:=false;
                    a:=i xor (1 shl z);
                    if (b=n) and (m1[z+1]=0)
                    then continue;
                    c:=m2[b-1]*m1[z+1];
                    for w:=0 to md-1 do
                       inc(m[i,(c+w) mod md],m[a,w]);
                 end;
     end;
  writeln(m[1 shl n-1,0]);
end.

{$R-,Q-,O+}
type mass=array[0..1 shl 18,0..99] of int64;
     maxx=array[0..18] of int64;
     majj=array[0..9] of boolean;
var used:majj;s:string;a,r,b,x,n,md,max1,res,c,k:int64;m:mass;m1,m2:maxx;w,i,j,z:longint;
begin
//  assign(input,'input.txt');
//  assign(output,'output.txt');
  readln(n,md);
  str(n,s);
  n:=length(s);
  for i:=1 to length(s) do
      m1[i]:=ord(s[i])-ord('0');
  m2[0]:=1;
  for i:=1 to length(s) do
     m2[i]:=m2[i-1]*10;
  for i:=1 to 1 shl n-1 do
     Begin
         b:=0;
         for z:=0 to n-1 do
            if (i and (1 shl z)>0)
            then inc(b);
         if b=1
         then Begin
                 for z:=0 to n-1 do
                    if (i and (1 shl z)>0)
                    then Begin
                            m[i,m1[z+1] mod md]:=1;
                            break;
                         end;
                 continue;
              end;
         fillchar(used,sizeof(used),true);
         for z:=0 to n-1 do
            if ((i shl z) and 1>0) and (used[m1[z+1]])
            then Begin
                    used[m1[z+1]]:=false;
                    a:=i xor (1 shl z);
                    if (b=n) and (m1[z+1]=0)
                    then continue;
                    c:=m2[b-1]*m1[z+1];
                    for w:=0 to md-1 do
                       inc(m[i,(c+w) mod md],m[a,w]);
                 end;
     end;
  writeln(m[1 shl n-1,0]);
end.

var k,n,m,x,y,z:int64;i,j,l:longint;
    a:array[0..4000000,0..100]of int64;
    p,f:array[0..25]of longint;
 begin
 //  assign(input,'input.txt');reset(input);
 // assign(output,'output.txt');rewrite(output);
    readln(n,m);
    k:=0;
    while n>0 do
     begin
      inc(k);p[k]:=n mod 10;
      n:=n div 10;
     end;
    z:=(1 shl k)-1;
    a[0,0]:=1;x:=0;
    for i:=0 to z do
     for j:=0 to m-1 do
      begin
       if a[i,j]=0 then continue;
       inc(x);
       for l:=1 to k do
        begin
         y:=1 shl (l-1);
         if(f[p[l]]<>x)and(i and y=0)and((i<>0)or(p[l]<>0))then
          begin
           f[p[l]]:=x;
           inc(a[i or y,(j*10+p[l])mod m],a[i,j]);
          end;
        end;
      end;
    writeln(a[z,0]);

 end.

{$r-,s-,q-,i-,o+}
var
  n,m,g,i,j,ost,l,vzyali:longint;s:ansistring;
  code : integer;
  c : char;
  f : array [0..200,0..200] of int64;

  function set1(m,b:int64):int64;begin exit(m or (1 shl ((length(s)-b+1)-1)));end;
  function get(m,b:int64):int64;begin exit(ord((m and (1 shl ((length(s)-b+1)-1))=(1 shl ((length(s)-b+1)-1)))));end;
  function newost(x,y:int64):int64;var res:int64;begin res:=x;res:=(10*res)mod m;res:=(y+res)mod m;exit(res mod m); end;
  function cifra(c:char):longint;var res:longint;code:integer;begin val(c,res,code);exit(res);end;

begin
  readln(n,m);str(n,s);
  g:=length(s);
  fillchar(f,sizeof(f),0);
  f[0,0]:=1;
  for i:=0 to (1 shl g)-1 do
    for ost:=0 to m-1 do
    if f[i,ost]>0 then
    begin
     for c:='0' to '9' do
        begin
          if(c='0')and(i=0)then continue;
          val(c,j,code);
          for l:=1 to g do
            if (s[l]=c) and (get(i,l)=0) then
              begin
                f[set1(i,l),newost(ost,j)]:=f[set1(i,l),newost(ost,j)]+f[i,ost];
                break;
              end;
        end;
    end;
  writeln(f[(1 shl g)-1,0]);
end.

{$r-,s-,q-,i-,o+}
var
  n,m,g,i,j,ost,l,vzyali:longint;s:ansistring;
  code : integer;
  c : char;
  f : array [0..200,0..200,0..200] of int64;

  function set1(m,b:int64):int64;begin exit(m or (1 shl ((length(s)-b+1)-1)));end;
  function get(m,b:int64):int64;begin exit(ord((m and (1 shl ((length(s)-b+1)-1))=(1 shl ((length(s)-b+1)-1)))));end;
  function newost(x,y:int64):int64;var res:int64;begin res:=x;res:=(10*res)mod m;res:=(y+res)mod m;exit(res mod m); end;
  function cifra(c:char):longint;var res:longint;code:integer;begin val(c,res,code);exit(res);end;

begin
  readln(n,m);str(n,s);
  g:=length(s);
  fillchar(f,sizeof(f),0);
  f[0,0,0]:=1;
  for i:=0 to 1 shl g-1 do
    for ost:=0 to m-1 do
      for vzyali:=0 to g do
    if f[i,ost,vzyali]>0 then
    begin
     for c:='0' to '9' do
        begin
          if(c='0')and(vzyali=0)then continue;
          val(c,j,code);
          for l:=1 to g do
            if (s[l]=c) and (get(i,l)=0) then
              begin
                f[set1(i,l),newost(ost,j),vzyali+1]:=f[set1(i,l),newost(ost,j),vzyali+1]+f[i,ost,vzyali];
                break;
              end;
        end;
    end;
  writeln(f[1 shl g-1,0,g]);
end.

const fi=''; fo='';

Var n,m:longint;
    last:longword;
    d:array[0..(1 shl 18),0..20] of Qword;
    pow:array[0..20] of Qword;
    list:array[0..20] of byte;
    A,B:array[0..20] of byte;
    dd:array[0..10] of boolean;
    vt:array[0..10] of Longint;

Procedure doc;
Var kk:Qword; i:longint;
begin
    assign(input,fi); reset(input);
    Readln(KK,m);
    while (kk<>0) do
    begin
        inc(n);
        B[n]:=kk mod 10;
        kk:=kk div 10;
    end;
    for i:=1 to n do
        A[i]:=B[n-i+1];
end;

Function getbit(x:longword; i:byte):byte;
begin
    getbit:=(x Shr i) and 1;
end;

Function bitoff(x:longword; i:byte):longword;
begin
    bitoff:=x and (not (1 Shl i));
end;

procedure xuly;
Var s,p:longword;
    i,j,k,kk,k1:longint;
    x:Qword;  val:Longint;
begin
    pow[1]:=1;
    for i:=2 to n do
        pow[i]:=pow[i-1]*10;
    last:=(1 Shl n)-1;
    for s:=1 to last do
        begin
            for i:=0 to 9 do
                dd[i]:=false;
            k:=0;
             for i:=0 to n-1 do
                 if getbit(s,i)=1 then
                  begin
                     inc(k);
                     dd[a[i+1]]:=true;
                     vt[a[i+1]]:=i;
                     end;
             for i:=0 to 9 do
              if dd[i] then
               If (k<>n) or (i<>0) then
                 begin
                     j:=vt[i];
                     p:=bitoff(s,j);
                     x:=(pow[k]*i) mod m;
                     for kk:=0 to m do
                         begin
                             k1:=(x+kk) mod m;
                             inc(d[s,k1],d[p,kk]);
                         end;
                 end;
        end;
end;

procedure ghi;
begin
     assign(output,fo); rewrite(output);
     writeln(d[last,0]);
end;

begin
    doc;
    D[0,0]:=1;
    xuly;
    ghi;
end.

{$mode objFpc}
uses math;
const
                finp            =               '';
                fout            =               '';
                maxcs           =               18;
type
                cs              =               array[0..20] of integer;
var
                fi,fo           :               text;
                n               :               int64;
                m,sl,vc         :               integer;
                x               :               cs;
                fx              :               array[0..262500,0..110] of int64;
                gt              :               array[0..20] of int64;
                dd              :               array[0..9] of integer;
procedure openfile;
begin
        assign(fi,finp);reset(fi);
        assign(fo,fout);rewrite(fo);
end;
procedure closefile;
begin
        close(fi);close(fo);
end;
procedure enter;
var
        i               :               integer;
begin
        readln(fi,n,m);
end;
procedure swap(t:integer);
begin
        sl:=0;
        repeat
                inc(sl);
                x[sl]:=t mod 10;
                t:=t div 10;
        until t=0;
end;
function get(t,x:integer):integer;
begin
        result:=(t>>(x-1)) and 1;
end;
procedure process;
var
        i,j,t,k             :               integer;
        r                   :               int64;
begin
        swap(n);
        gt[0]:=1;
        for i:=1 to 18 do
                gt[i]:=gt[i-1]*i;
        fx[0,0]:=1;
        vc:=(1<<sl) -1;
        for i:=0 to vc do
        for k:=0 to m do
        begin
                for j:=1 to sl do
                begin
                        if (x[j]=0) and (i=0) then continue;
                        if get(i,j)=0 then
                        begin
                                t:=i or (1<<(j-1));
                                inc(fx[t,(k*10+x[j]) mod m],fx[i,k]);
                        end;
                end;
        end;
        r:=fx[vc,0];
        for i:=1 to sl do
                inc(dd[x[i]]);
        for i:=0 to 9 do
                r:=r div gt[dd[i]];
        writeln(fo,r);
end;
begin
        openfile;
        enter;
        process;
        closefile;
end.


       uses strutils;
       const eps=1e-6;
       besk=2000000000000000000; label 4,1,3,2,44,22;
       type mas=array[-200000..2600000+1] of int64;
       integer=longint;     zap=record  x,y,z:int64; end;
       zap1=^zap;
       var kk,oldpos,jj,kolvzv,sdvig,i1,j1,mask,stepp,n2,i,j,k,l,step,k1,mesto:longint;
       oldss,smin,ss3,ss,ss1,ss2:string;
       aans:zap;
       pqqq:zap;  disk,xxx,yyy,prop,dl,bet,al:extended; fl1,fl2,fl:boolean;
       drans,dr3,dr1,dr2,pt,ott1,ott2:zap;
       tt1,tt2,shag,radius,ote,ddl,dl1,dl2:extended;

       xx,yy:array[-1..5000] of extended;
       xstart,ystart,ax,ay,bx,by,aaa,bbb,ccc,bb,aa,ot,ii,p0,t,pos,xmax,xmin,ymax,ymin,newpos,newdl,len,pp3,ans1,ans2,modul,t0,t1,t2,dloch,koldel,mmax,nap,stepen,pq,pp1,pp2,period, dolg,ot1,ot2,oldgr,predsum,pmin,tmin,kool,number,imin,qq,kol,chisl,znam,n,m,ttt,numpos,vx,vy,ans,up,down,imax,t3,qq1,qq2,q1,q2,ost,koll,w,dx,dy,time,p3,p4,qqqq,tt,pmax,pmax2,p1,p2,ott,hh,pos0,pos1,pos2,pp,mm,maxot,tmax,nn,n1,q,p:int64; flag1:boolean;
       x,zzz,fact,z,stan,a2,invx,invy,summ,place,str,b2,z1,sum1,sum0,d0,d1,c,d,a,b:mas; flag2,flag:boolean;  pl:longint;
       ot3,izb, sum,oldmax,newmax,posmax,zapas,razn,oldll,oldrr,ppp,posx,posy,minx,miny,maxx,maxy,kolplus,kolminus,razr,jmax,gr,grr,ener,dob,cc,dist,ugg,aa1,aa2,r,ug:int64;
       new,zan2,zan1,prost,marked,zan:array[-10000..11000000] of boolean;
       left,right,newoch,d2,och, num,start,finish,zz,ll,rr,path,lx,a3,b3,predok,a1,b1:array[-200000..955000] of int64;
       dd,koldelit,delit:array[0..800001,0..6] of longint;
       ppole1,ppole2,ppole:array[-10000..3000000] of char;
       bufer, str1,str2,stroka:array[-1000..1000000] of char;
       dinam:array[-100000..1000000] of extended;
       al1,bet1,cosa,sina,cosb,sinb,rx,ry,px,py,h1,h2,h,l1,l2,otmax,v:int64;
       sss1,sss2,sp,sss:array[-10..1100005] of string;
       newpole,pole:array[-1..1100,-1..1100] of char;
       sumstr,sumstolb,nomerpole:array[-1..1100,-1..1100] of longint;
       verch,verzn:array[-10..1100000] of int64;
       ddmax,dd1,dd2,ddinv:array[-2..1100000] of int64;
       zzan:array[-1..1200000] of boolean;
       obmen,ch:char;
       ff,flagg1:boolean;
       newleft,newright,pred,oldx,oldy,numx,numy,xleft,xright,s,znach,newplace,newstr,next,prev,aold,y:array[-100000..1700002] of int64;
       function srav(i,j:longint):boolean; label 44;
            begin
                if (a[i]<a[j]) then begin srav:=true; goto 44;  end;
                if a[i]>a[j] then begin  srav:=false; goto 44;   end;
                if a[i]=a[j] then if a2[i]<a2[j] then srav:=true else srav:=false;
                    44:
             end;

         procedure sl (k,l,m:integer);  {k- dlina, l,m - nachalo dvuh blokov}
        begin
           i:=l;j:=m; flag:=true; step:=0;while flag do begin
           if (srav(i,j)) then begin begin b[l+step]:=a[i];b1[l+step]:=a1[i];b2[l+step]:=a2[i];b3[l+step]:=a3[i];end;
           inc(step);inc(i);
          {writeln('step=',step,' i=',i);} end;
           if (not srav(i,j)) then begin begin b[l+step]:=a[j];b1[l+step]:=a1[j];b2[l+step]:=a2[j];b3[l+step]:=a3[j];end;
           inc(step);inc(j);end;
           if  ((i>=l+k) or (j>=m+k)) then flag:=false;
           end;
           if(i>=l+k) then repeat b[l+step]:=a[j];b1[l+step]:=a1[j];b2[l+step]:=a2[j]; b3[l+step]:=a3[j];
              inc(step);inc(j);until (j>=m+k);
              if(j>=m+k) then repeat b[l+step]:=a[i];b1[l+step]:=a1[i];b2[l+step]:=a2[i];
              b3[l+step]:=a3[i];inc(step);inc(i);until (i>=l+k);
        end;

        procedure sort(var c:mas);
            begin
              k:=1; repeat
              l:=1;m:=k+l; repeat sl(k,l,m); l:=l+2*k;m:=m+2*k; until (l>=n); k:=k*2;
              for i:=1 to n do begin a[i]:=b[i];a1[i]:=b1[i];a2[i]:=b2[i];a3[i]:=b3[i]; end;
              until (k>=n);
            end;

        function min (a,b:int64):int64;begin min:=b;if a<b then min:=a;  end;
        function max (a,b:int64):int64;begin max:=b;if a>b then max:=a;  end;

      {   function evkl(a,b:int64):zap1; var d,x,y,r,q,i,j,k,x1,x2,y1,y2:int64;
           tt:zap; tt1:zap1;  fl:boolean;
           begin
             fl:=false;
             if a<b then begin r:=a;a:=b;b:=r; fl:=true; end;
             x2:=1;x1:=0;y2:=0;y1:=1;
             while b>0 do
             begin q:=a div b; r:=a mod b; x:=x2-q*x1;y:=y2-q*y1;
             a:=b;b:=r;x2:=x1;x1:=x;y2:=y1;y1:=y;
             end;
             if not fl then begin d:=a; x:=x2;y:=y2; tt.p1:=d;tt.p2:=x;tt.p3:=y; tt1:=@tt; end else
             begin d:=a; x:=x2;y:=y2; tt.p1:=d;tt.p2:=y;tt.p3:=x; tt1:=@tt; end;
             evkl:=tt1;
           end;

                          }
      function phi(a,b:double):double; label 222;var p:double;
         begin
           if (a>0)and (b>=0) then begin p:=arctan(b/a);goto 222; end;
           if (a<0) then begin p:=pi+arctan(b/a);goto 222; end;
           if (a>0)and (b<0) then begin p:=2*pi+arctan(b/a);goto 222; end;
           if (a=0)and (b>0) then begin p:=pi/2;goto 222; end;
           if (a=0)and (b<0) then begin p:=3*pi/2;goto 222; end;
           222: phi:=p;
         end;

         function nod(a,b:int64):int64; var p,t:int64;
         begin
            a:=abs(a); b:=abs(b);
            if (a>0)and(b>0) then
            begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
                p:=a;
               end else if a=0 then p:=b else p:=a;
                 nod:=p;
         end;
       function arccos(tt:extended):extended;
        begin
          if abs(tt)>1-1e-13 then
          if tt>0 then  arccos:=0 else arccos:=pi;
          if tt=0 then arccos:=pi/2 else
          if abs(tt)<=1-1e-13 then if tt>=0 then arccos:=arctan(sqrt(1-tt*tt)/tt)
          else arccos:=pi+arctan(sqrt(1-tt*tt)/tt);

        end;

        function degg(a:int64;k:int64):int64; var p:int64;    label 4;
          begin
            if k<=0 then degg:=1
            else if odd(k) then degg:=(degg(a,k-1)*a) mod modul  else
            begin  p:=degg(a,k div 2);  degg:=(p*p) mod modul;    end;
          end;

           function zaprmax(left,right:longint):int64;
         begin
          if left>=right then zaprmax:=y[left] else
             begin
               if not odd(left) and odd(right) then zaprmax:=zaprmax(left shr 1,right shr 1) else
               begin
                if odd(left) then zaprmax:=max(zaprmax(left+1,right),y[left]);
                if not odd(right) then zaprmax:=max(zaprmax(left,right-1),y[right]);
               end;
              end;

         end;

       function zaprmin(left,right:longint):zap1;
        var nomer,ot,ii,jj:int64; pp,ans:zap;
            begin
             if left>=right then begin  ans.x:=y[left]; ans.y:=z[left]; zaprmin:=@ans; end  else
               begin
                if not odd(left) and odd(right) then
                 begin
                 zaprmin:=zaprmin(left shr 1,right shr 1)
                end  else
                   begin
                     if odd(left) then
                       begin
                  { zaprmin:=min(zaprmin(left+1,right),y[left]); }
                     pp:=zaprmin(left+1,right)^;
                     if pp.x<y[left] then begin ans.x:=pp.x; ans.y:=pp.y; end
                     else begin ans.x:=y[left]; ans.y:=z[left];  end;
                    zaprmin:=@ans;
                    end;
                 if not odd(right) then {zaprmin:=min(zaprmin(left,right-1),y[right]); }
                    begin
                      { zaprmin:=min(zaprmin(left+1,right),y[left]); }
                       pp:=zaprmin(left,right-1)^;
                       if pp.x<y[right] then begin ans.x:=pp.x; ans.y:=pp.y; end
                        else begin ans.x:=y[right]; ans.y:=z[right];  end;
                      zaprmin:=@ans;
                     end;
                    end;
                  end;

            end;

      procedure udal(ii:longint);
       begin  next[prev[ii]]:=next[ii];prev[next[ii]]:=prev[ii];  end;

       function nomerr(gr:int64):int64;
       var ot,aa,bb,cc:int64; fl:boolean;
          begin
            aa:=1; bb:=qq;  cc:=aa; fl:=false;
            while abs(aa-bb)>1 do
              begin
              cc:=(aa+bb) shr 1;
             {  writeln('cc=++ ',cc);
             } if x[cc]<gr then aa:=cc else bb:=cc;

              end;
          {  writeln(' cc= ',cc);
            } cc:=max(cc-3,0);
             repeat inc(cc) until x[cc]>=gr;
             nomerr:=cc;
          end;

        function pprost(pp:int64):boolean;
        var i,j,t:longint; tt:extended; fl:boolean;
          begin
          tt:=pp;
          tt:=sqrt(tt);
          fl:=true;
          for i:=2 to round(tt) do if pp mod i=0 then  fl:=false;
          pprost:=fl;
          end;

     function gl(c:char):boolean;
         begin
        if (c='a')or (c='o')or(c='e')or(c='i')or(c='u') then gl:=true else gl:=false;
         end;

     function slog(d1,d2:zap):zap1;
       var t,tt,p,q:int64; ans:zap;
         begin
            p:=d1.x*d2.y+d1.y*d2.x;
            q:=d1.y*d2.y;
            t:=nod(p,q);
            p:=p div t; q:=q div t;
            ans.x:=p mod modul;ans.y:=q mod modul;
            slog:=@ans;
         end;

   function umn(d1,d2:zap):zap1;
       var t,tt,p,q:int64; ans:zap;
         begin
          p:=d1.x*d2.x;
          q:=d1.y*d2.y;
          t:=nod(p,q);
          p:=p div t; q:=q div t;
          ans.x:=p mod modul;ans.y:=q mod modul;
          umn:=@ans;
         end;

       function bincoef(ii,jj:longint):int64;
          var t1,t2,t3,p1,p2,p3,ot:int64;
          begin
            p1:=fact[ii];
            p2:=fact[jj];
            p3:=fact[ii-jj];
            t2:=degg(p2,modul-2);
            t3:=degg(p3,modul-2);
            ot:=((p1*t2) mod modul*t3) mod modul;
            bincoef:=ot;
          end;

function stoim(lavel:int64):int64;
var i,j:longint; tt1,sumplus,summinus,tt,ot:int64;
begin
  sumplus:=0;
  summinus:=0;
  for i:=1 to p do
    begin
       if a[i]>lavel then sumplus:=sumplus+a[i]-lavel;
       if a[i]<lavel then summinus:=summinus+lavel-a[i];
    end;
 {  writeln('lavel= ',lavel,'  sum+= ',summinus,' sum-= ' ,sumplus);
  }  tt:=min(sumplus,summinus)*cc;
       if sumplus>summinus then tt1:=(sumplus-summinus)*bb else
       tt1:=(summinus-sumplus)*aa;
       ot:=tt+tt1;
       stoim:=ot;
end;

    begin
     { assign (input,'D:\Projects\input.txt');reset(input);
      assign (output,'D:\Projects\output.txt'); rewrite(output);
      }
  read(x[0],y[0],ax,ay,bx,by);
  read(xstart,ystart,time);
  for i:=1 to 100 do
    begin
      x[i]:=ax*x[i-1]+bx;
      y[i]:=ay*y[i-1]+by;
      inc(p);
      if x[i]+y[i]>40000000000000000 then goto 2;

    end;
2:
     {  for i:=0 to p do writeln(x[i],' ',y[i]);
     }           ot:=0;
     for i:=0 to p do for j:=i to p do
        begin
           t1:=min(abs(x[i]-xstart)+abs(y[i]-ystart),abs(x[j]-xstart)+abs(y[j]-ystart));
           t2:=abs(x[i]-x[j])+abs(y[i]-y[j]);
           if t1+t2<=time then ot:=max(ot,j-i+1);
        end;

  writeln(ot);

           1: close (output);
    end.

const jie=50000000000000000;
var
  x,y:array[0..1000] of int64;
  ax,ay,bx,by,xs,ys,t,time:int64;
  n,i,ans,anss,j:longint;
begin
  readln(x[0],y[0],ax,ay,bx,by);
  readln(xs,ys,t);
  n:=100;
  for i:=1 to n do
  begin
    x[i]:=x[i-1]*ax+bx;
    y[i]:=y[i-1]*ay+by;
    if (x[i]>=jie) or (y[i]>=jie) then break;
  end;
  n:=i-1;
  for i:=0 to n do
  begin
    if anss>ans then ans:=anss;
    anss:=0;
    time:=abs(xs-x[i])+abs(ys-y[i]);
    if time>t then continue;
    inc(anss);
    for j:=i-1 downto 0 do
    begin
      time:=time+abs(x[j+1]-x[j])+abs(y[j+1]-y[j]);
      if time>t then break;
      inc(anss);
    end;
    time:=time+x[i+1]-x[0]+y[i+1]-y[0];
    if time>t then continue;
    inc(anss);
    for j:=i+1 to n-1 do
    begin
      time:=time+abs(x[j+1]-x[j])+abs(y[j+1]-y[j]);
      if time>t then break;
      inc(anss);
    end;
  end;
  if anss>ans then ans:=anss;
  writeln(ans);
end.

const jie=100000000000000000;
var
  x,y:array[0..100] of int64;
  ax,ay,bx,by,xs,ys,t,time:int64;
  n,i,ans,anss,j:longint;
begin
  readln(x[0],y[0],ax,ay,bx,by);
  readln(xs,ys,t);
  n:=100;
  for i:=1 to n do
  begin
    x[i]:=x[i-1]*ax+bx;
    y[i]:=y[i-1]*ay+by;
    if (x[i]>=jie) or (y[i]>=jie) then break;
  end;
  n:=i-1;
  for i:=0 to n do
  begin
    if anss>ans then ans:=anss;
    anss:=0;
    time:=abs(xs-x[i])+abs(ys-y[i]);
    if time>t then continue;
    inc(anss);
    for j:=i-1 downto 0 do
    begin
      time:=time+abs(x[j+1]-x[j])+abs(y[j+1]-y[j]);
      if time>t then break;
      inc(anss);
    end;
    time:=time+x[i+1]-x[0]+y[i+1]-y[0];
    if time>t then continue;
    inc(anss);
    for j:=i+1 to n-1 do
    begin
      time:=time+abs(x[j+1]-x[j])+abs(y[j+1]-y[j]);
      if time>t then break;
      inc(anss);
    end;
  end;
  if anss>ans then ans:=anss;
  writeln(ans);
end.

const jie=100000000000000000;
var
  x,y:array[0..100] of int64;
  ax,ay,bx,by,xs,ys,t,time:int64;
  n,i,ans,anss,j:longint;
begin
  readln(x[0],y[0],ax,ay,bx,by);
  readln(xs,ys,t);
  n:=100;
  for i:=1 to n do
  begin
    x[i]:=x[i-1]*ax+bx;
    y[i]:=y[i-1]*ay+by;
    if (x[i]>=jie) or (y[i]>=jie) then break;
  end;
  n:=i-1;
  for i:=0 to n do
  begin
    if anss>ans then ans:=anss;
    anss:=0;
    time:=abs(xs-x[i])+abs(ys-y[i]);
    if time>t then continue;
    inc(anss);
    for j:=i-1 downto 0 do
    begin
      time:=time+abs(x[j+1]-x[j])+abs(y[j+1]-y[j]);
      if time>t then break;
      inc(anss);
    end;
    time:=time+x[i+1]-x[1]+y[i+1]-y[1];
    if time>t then continue;
    inc(anss);
    for j:=i+1 to n-1 do
    begin
      time:=time+abs(x[j+1]-x[j])+abs(y[j+1]-y[j]);
      if time>t then break;
      inc(anss);
    end;
  end;
  writeln(ans);
end.