#include <bits/stdc++.h>
using namespace std;
constexpr int N = (1 << 18) + 10;
constexpr int M = 100 + 10;
long long m, x, dp[N][M], h, n, fact[M], p, sz;
vector<int> digit;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  while (n) {
    digit.push_back(n % 10);
    n /= 10;
  }
  sz = (int)digit.size();
  fact[0] = 1;
  for (int i = 1; i < M; i++) fact[i] = fact[i - 1] * i;
  x = 1 << sz;
  for (int i = 0; i < sz; i++)
    if (digit[i]) dp[1 << i][digit[i] % m]++;
  for (int mask = 1; mask < x; mask++) {
    for (int i = 0; i < m; i++) {
      p = 1;
      for (int j = 0; j < sz; j++) {
        if (p & mask) {
          p *= 2;
          continue;
        }
        dp[mask + p][((10 * i) + digit[j]) % m] += dp[mask][i];
        p *= 2;
      }
    }
  }
  for (int i = 0; i < 10; i++) {
    int cnt = 0;
    for (auto j : digit)
      if (j == i) cnt++;
    dp[x - 1][0] /= fact[cnt];
  }
  cout << dp[x - 1][0] << '\n';
  return 0;
}
