#include <bits/stdc++.h>
long long solve(std::string s, int mod) {
  const int n = (int)s.size();
  std::vector<std::vector<long long>> cnt(1 << n,
                                          std::vector<long long>(mod, 0));
  std::vector<long long> pow10(1 + n, 1 % mod);
  for (int i = 1; i <= n; ++i) {
    pow10[i] = pow10[i - 1] * 10 % mod;
  }
  cnt[0][0] = 1;
  for (int mask = 1; mask < (1 << n); ++mask) {
    auto pow = pow10[__builtin_popcount(mask) - 1];
    for (int j = 0; j < n; ++j) {
      if (!((mask >> j) & 1)) continue;
      auto fromMask = mask ^ (1 << j);
      for (int rem = 0; rem < mod; ++rem) {
        auto nRem = (rem + (s[j] - '0') * pow) % mod;
        cnt[mask][nRem] += cnt[fromMask][rem];
      }
    }
  }
  long long answ = 0;
  for (int j = 0; j < n; ++j) {
    if (s[j] == '0') continue;
    auto mask = ((1 << n) - 1);
    mask &= ~(1 << j);
    auto pow = pow10[__builtin_popcount(mask)];
    auto rem = (mod - ((s[j] - '0') * pow) % mod) % mod;
    answ += cnt[mask][rem];
  }
  std::vector<long long> fact(1 + n, 1);
  for (int i = 1; i <= n; ++i) {
    fact[i] = fact[i - 1] * i;
  }
  std::vector<long long> nDigits(10);
  for (auto& it : s) {
    nDigits[it - '0']++;
  }
  for (int d = 0; d < 10; ++d) {
    answ /= fact[nDigits[d]];
  }
  return answ;
}
int main() {
  std::string s;
  int mod;
  while (std::cin >> s >> mod) {
    std::cout << solve(s, mod) << std::endl;
  }
  return 0;
}
