#include <bits/stdc++.h>
using namespace std;
int l = -1;
long long N, M, f[262150][105], w[20], sum[10], jc[20];
int main() {
  cin >> N >> M;
  for (; N; N /= 10) w[++l] = N % 10;
  for (int i = 0; i <= l; ++i) {
    sum[w[i]]++;
    if (w[i]) f[1 << i][w[i] % M] = 1;
  }
  for (int i = 1; i <= (1 << l + 1) - 1; ++i) {
    for (int k = 0; k <= M - 1; ++k)
      for (int j = 0; j <= l; ++j) {
        if ((1 << j ^ i) > i) f[1 << j ^ i][(k * 10 + w[j]) % M] += f[i][k];
      }
  }
  long long ans = f[(1 << l + 1) - 1][0];
  jc[0] = 1;
  for (int i = 1; i <= l + 1; ++i) jc[i] = jc[i - 1] * i;
  for (int i = 0; i <= 9; ++i) ans /= jc[sum[i]];
  cout << ans << endl;
}
