#include <bits/stdc++.h>
using namespace std;
long long n, m, f[(1 << 18) + 10][105], fac[20];
int c[20], cnt[10], tot;
void init() {
  while (n) {
    int x = n % 10;
    c[tot++] = x;
    cnt[x]++;
    n /= 10;
  }
  sort(c, c + tot);
  fac[0] = 1;
  for (int i = 1; i < 20; i++) fac[i] = fac[i - 1] * i;
}
int main() {
  cin >> n >> m;
  init();
  int start = 0;
  for (int i = 0; i < tot; i++)
    if (c[i] == 0)
      start = i + 1;
    else
      f[1 << i][c[i] % m] = 1;
  for (int s = (1 << start); s < (1 << tot); s++) {
    for (int j = 0; j < tot; j++)
      if ((1 << j) & s) {
        for (int k = 0; k <= m; k++)
          f[s][(k * 10 + c[j]) % m] += f[s ^ (1 << j)][k];
      }
  }
  long long ans = f[(1 << tot) - 1][0];
  for (int i = 0; i <= 9; i++) ans /= fac[cnt[i]];
  cout << ans << endl;
  return 0;
}
