#include <bits/stdc++.h>
using namespace std;
const int W = 18, M = 100;
int m, len;
long long n, f[1 << W | 7][M | 7];
vector<int> d;
inline void Pre() { memset(f, -1, sizeof f); }
inline long long Dfs(register int w, register int st, register int sum) {
  if (!w) return sum == 0;
  if (~f[st][sum]) return f[st][sum];
  register long long res = 0;
  for (register int i = 0; i < len; i++)
    if (!((1 << i) & st) &&
        (i == 0 || d[i] != d[i - 1] || ((1 << (i - 1)) & st)))
      res += Dfs(w - 1, st | (1 << i), (sum * 10 + d[i]) % m);
  return f[st][sum] = res;
}
inline long long DP() {
  for (; n; n /= 10) d.push_back(n % 10);
  sort(d.begin(), d.end()), len = d.size();
  register long long res = 0;
  for (register int i = 0; i < len; i++)
    if (d[i] && (i == 0 || d[i] != d[i - 1]))
      res += Dfs(len - 1, 1 << i, d[i] % m);
  return res;
}
int main() {
  scanf("%lld%d", &n, &m), Pre();
  printf("%lld\n", DP());
  return 0;
}
