#include <bits/stdc++.h>
using namespace std;
const long long N = 20;
long long n, m;
long long f[11];
vector<long long> v;
long long dp[1 << 18][100];
long long fact[N];
void precompute() {
  fact[0] = fact[1] = 1;
  for (long long i = 2; i < N; i++) {
    fact[i] = fact[i - 1] * i;
  }
}
void work(long long k) {
  while (k > 0) {
    v.push_back(k % 10);
    f[k % 10]++;
    k /= 10;
  }
  n = v.size();
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  precompute();
  cin >> n >> m;
  work(n);
  for (long long rem = 0; rem < m; rem++) dp[(1 << n) - 1][rem] = (rem == 0);
  for (long long mask = (1 << n) - 2; mask >= 0; mask--) {
    for (long long rem = 0; rem < m; rem++) {
      long long &ans = dp[mask][rem];
      if (mask == 0) {
        for (long long i = 0; i < n; i++) {
          if (v[i] == 0 || (mask >> i) & 1) continue;
          long long cur = v[i];
          ans += dp[mask | (1 << i)][(rem * 10 + cur) % m];
        }
      } else {
        for (long long i = 0; i < n; i++) {
          if ((mask >> i) & 1) continue;
          long long cur = v[i];
          ans += dp[mask | (1 << i)][(rem * 10 + cur) % m];
        }
      }
    }
  }
  long long ans = dp[0][0];
  for (long long i = 0; i <= 9; i++) ans /= fact[f[i]];
  cout << ans;
  return 0;
}
