#include <bits/stdc++.h>
using namespace std;
namespace patch {
template <typename T>
std::string to_string(const T& n) {
  std::ostringstream stm;
  stm << n;
  return stm.str();
}
}  // namespace patch
int m, lim, len;
long long dp[1 << 18][101];
string s;
long long fac[19];
int freq[10];
long long solve(int mask, int mod) {
  if (mask == lim) {
    if (mod == 0) return 1;
    return 0;
  }
  if (dp[mask][mod] != -1) return dp[mask][mod];
  long long ans = 0;
  for (int i = 0; i < len; ++i) {
    if (!(mask & (1 << i))) {
      if (mask == 0) {
        if (s[i] == '0') continue;
      }
      ans += solve(mask | (1 << i), (mod * 10 + (s[i] - '0')) % m);
    }
  }
  return dp[mask][mod] = ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  fac[0] = fac[1] = 1;
  for (int i = 2; i <= 18; ++i) fac[i] = fac[i - 1] * (long long)i;
  memset(dp, -1, sizeof dp);
  cin >> s >> m;
  len = s.size();
  for (int i = 0; i < len; ++i) {
    freq[s[i] - '0']++;
  }
  lim = (1 << len) - 1;
  long long ans = solve(0, 0);
  for (int i = 0; i < 10; ++i) {
    if (freq[i]) ans /= fac[freq[i]];
  }
  cout << ans << '\n';
  return 0;
}
