#include <bits/stdc++.h>
using namespace std;
string s;
long long n, mod, fact[18], dp[1 << 18][100], cnt[26], ans;
int main() {
  cin >> s >> mod;
  n = s.size();
  fact[0] = dp[0][0] = 1;
  for (int i = 1; i < 18; ++i) fact[i] = fact[i - 1] * i;
  for (auto &it : s) ++cnt[it - '0'];
  for (int mask = 0; mask < (1 << n); ++mask) {
    for (int i = 0; i < mod; ++i) {
      if (dp[mask][i] == 0) continue;
      for (int d = 0; d < n; ++d) {
        if (mask == 0 && s[d] == '0') continue;
        if (mask & (1 << d)) continue;
        dp[mask | (1 << d)][(i * 10 + s[d] - '0') % mod] += dp[mask][i];
      }
    }
  }
  ans = dp[(1 << n) - 1][0];
  for (int i = 0; i <= 9; ++i) ans /= max(1ll, fact[cnt[i]]);
  cout << ans;
  return 0;
}
