#include <bits/stdc++.h>
using namespace std;
long long int i, j, n, m, ans, freq[20], dp[1 << 18][120], limit, mask, temp, d,
    fact[100];
vector<long long int> v;
int main() {
  cin >> n >> m;
  temp = n;
  while (temp) {
    d = temp % 10;
    freq[d]++;
    temp = temp / 10;
    v.push_back(d);
  }
  fact[0] = 1;
  for (i = 1; i <= 19; i++) fact[i] = i * fact[i - 1];
  dp[0][0] = 1;
  limit = 1 << v.size();
  for (mask = 0; mask < limit; mask++) {
    for (i = 0; i < v.size(); i++) {
      for (j = 0; j < m; j++) {
        if (mask & (1 << i)) continue;
        if (mask == 0 && v[i] == 0) continue;
        dp[mask | (1 << i)][(j * 10 + v[i]) % m] += dp[mask][j];
      }
    }
  }
  ans = dp[limit - 1][0];
  for (i = 0; i < 10; i++) ans = ans / fact[freq[i]];
  cout << ans;
  return 0;
}
