#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, long long> ll;
typedef vector<long long> vl;
typedef vector<ll> vll;
typedef vector<vl> vvl;
template <typename T>
ostream &operator<<(ostream &o, vector<T> v) {
  if (v.size() > 0) o << v[0];
  for (unsigned i = 1; i < v.size(); i++) o << " " << v[i];
  return o << " ";
}
template <typename U, typename V>
ostream &operator<<(ostream &o, pair<U, V> p) {
  return o << "(" << p.first << ", " << p.second << ") ";
}
template <typename T>
istream &operator>>(istream &in, vector<T> &v) {
  for (unsigned i = 0; i < v.size(); i++) in >> v[i];
  return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &p) {
  in >> p.first;
  in >> p.second;
  return in;
}
int main(int argc, char *argv[]) {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(20);
  string s;
  long long m;
  cin >> s >> m;
  vl fact(20, 1);
  for (long long i = (2); i < (long long)20; i++) fact[i] = fact[i - 1] * i;
  long long n = s.length();
  vvl dp((1 << n), vl(m, 0));
  dp[0][0] = 1;
  for (long long(mask) = 0; (mask) < ((1 << n)); (mask)++) {
    for (long long(rem) = 0; (rem) < (m); (rem)++) {
      if (dp[mask][rem] == 0) continue;
      for (long long i = (0); i < (long long)n; i++) {
        if (mask == 0 and s[i] == '0') continue;
        if (mask & (1 << i)) continue;
        long long x = (rem * 10 + s[i] - '0') % m;
        dp[mask | (1 << i)][x] += dp[mask][rem];
      }
    }
  }
  vl cnt(10, 0);
  for (long long i = (0); i < (long long)s.length(); i++) cnt[s[i] - '0']++;
  for (long long i = (0); i < (long long)cnt.size(); i++)
    dp[(1 << n) - 1][0] /= fact[cnt[i]];
  cout << dp[(1 << n) - 1][0] << "\n";
  return 0;
}
