#include <bits/stdc++.h>
const long long inf = 2e18;
using namespace std;
template <class T, class T2>
void smin(T& a, T2 val) {
  if (a > val) a = val;
}
template <class T, class T2>
void smax(T& a, T2 val) {
  if (a < val) a = val;
}
const int N = 5 * (int)1e5 + 10;
long long n, mod;
long long dp[1LL << 18][100];
vector<int> digits;
void solve() {
  cin >> n >> mod;
  vector<int> f(10);
  while (n) {
    digits.push_back(n % 10);
    f[digits.back()]++;
    n /= 10;
  }
  long long count = 1;
  for (int i = 0; i < (int)(digits.size()); ++i) {
    count *= 1LL * f[digits[i]];
    f[digits[i]]--;
  }
  n = (int)(digits.size());
  int mx = (1LL << n);
  dp[mx - 1][0] = 1;
  for (int msk = mx - 2; msk >= 0; --msk) {
    for (int rem = 0; rem < mod; ++rem) {
      for (int i = 0; i < n; ++i) {
        if (msk & (1LL << i)) {
          continue;
        }
        if (msk == 0 and digits[i] == 0) {
          continue;
        }
        dp[msk][rem] += dp[(msk | (1LL << i))][(rem * 10 + digits[i]) % mod];
      }
    }
  }
  long long ans = dp[0][0];
  ans /= count;
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
