#include <bits/stdc++.h>
using namespace std;
long long n, fact[19], dp[1 << 18][100];
int m, td, dig[18], f[10];
int main() {
  scanf("%lld %d", &n, &m);
  while (n) {
    dig[td++] = n % 10;
    f[n % 10]++;
    n /= 10;
  }
  for (int i = 0; i < td; i++) {
    if (dig[i] == 0) continue;
    dp[(1 << i)][dig[i] % m]++;
  }
  for (int mask = 0; mask < (1 << td); mask++) {
    for (int rem = 0; rem < m; rem++) {
      for (int j = 0; j < td; j++) {
        if (mask & (1 << j)) continue;
        dp[mask | (1 << j)][(rem * 10 + dig[j]) % m] += dp[mask][rem];
      }
    }
  }
  long long ans = dp[(1 << td) - 1][0];
  fact[0] = 1;
  for (int i = 1; i <= 18; i++) {
    fact[i] = i * fact[i - 1];
  }
  for (int i = 0; i < 10; i++) {
    ans /= fact[f[i]];
  }
  printf("%lld\n", ans);
  return 0;
}
