#include <bits/stdc++.h>
using namespace std;
inline long long Read() {
  long long res = 0, f = 1;
  char c;
  while (c = getchar(), c < 48 || c > 57)
    if (c == '-') f = 0;
  do res = (res << 3) + (res << 1) + (c ^ 48);
  while (c = getchar(), c >= 48 && c <= 57);
  return f ? res : -res;
}
template <class T>
inline bool Min(T &a, T const &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
inline bool Max(T &a, T const &b) {
  return a < b ? a = b, 1 : 0;
}
const long long N = 3e5 + 5, M = 3e5 + 5, mod = 1e9 + 7;
bool MOP1;
long long dp[5 + (1 << 18)][105], vis[11], Num[N];
bool MOP2;
inline void _main(void) {
  long long n = Read(), m = Read(), tot = 0;
  while (n) Num[++tot] = n % 10, n /= 10;
  dp[0][0] = 1;
  for (register long long i = (1), i_end_ = (1 << tot); i < i_end_; ++i) {
    memset(vis, 0, sizeof vis);
    for (register long long j = (1), j_end_ = (tot); j <= j_end_; ++j)
      if (i & (1 << (j - 1))) {
        if (!Num[j] && !(i ^ (1 << (j - 1)))) continue;
        if (vis[Num[j]]) continue;
        vis[Num[j]] = 1;
        for (register long long k = (0), k_end_ = (m); k < k_end_; ++k)
          dp[i][((k * 10) + Num[j]) % m] += dp[i ^ (1 << (j - 1))][k];
      }
  }
  printf("%lld\n", dp[(1 << tot) - 1][0]);
}
signed main() {
  _main();
  return 0;
}
