#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
long long n;
int m;
int c[10];
int i[10];
int d[18];
int main() {
  scanf("%I64d %d", &n, &m);
  for (int j = 0; j < 10; ++j) c[j] = 0;
  for (; n > 0; n /= 10) ++c[n % 10];
  long long f[c[0] + 1][c[1] + 1][c[2] + 1][c[3] + 1][c[4] + 1][c[5] + 1]
             [c[6] + 1][c[7] + 1][c[8] + 1][c[9] + 1][m];
  memset(f, 0, sizeof f);
  bool all = true;
  f[0][0][0][0][0][0][0][0][0][0][0] = 1;
  for (i[0] = 0; i[0] <= c[0]; ++i[0])
    for (i[1] = 0; i[1] <= c[1]; ++i[1])
      for (i[2] = 0; i[2] <= c[2]; ++i[2])
        for (i[3] = 0; i[3] <= c[3]; ++i[3])
          for (i[4] = 0; i[4] <= c[4]; ++i[4])
            for (i[5] = 0; i[5] <= c[5]; ++i[5])
              for (i[6] = 0; i[6] <= c[6]; ++i[6])
                for (i[7] = 0; i[7] <= c[7]; ++i[7])
                  for (i[8] = 0; i[8] <= c[8]; ++i[8])
                    for (i[9] = 0; i[9] <= c[9]; ++i[9]) {
                      for (int j = 0; j < m; ++j)
                        for (int k = 0; k < 10; ++k)
                          if (i[k] < c[k] and (!all or (all and k))) {
                            long long add = f[i[0]][i[1]][i[2]][i[3]][i[4]]
                                             [i[5]][i[6]][i[7]][i[8]][i[9]][j];
                            ++i[k];
                            f[i[0]][i[1]][i[2]][i[3]][i[4]][i[5]][i[6]][i[7]]
                             [i[8]][i[9]][(j * 10 + k) % m] += add;
                            --i[k];
                          }
                      all = false;
                    }
  printf("%I64d\n",
         f[c[0]][c[1]][c[2]][c[3]][c[4]][c[5]][c[6]][c[7]][c[8]][c[9]][0]);
  return 0;
}
