#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 18;
long long ans[N][100];
long long tot, a[25], m, n;
long long dfs(int st, int yu, int flag) {
  if (st == (1 << tot) - 1) return yu == 0;
  if (!flag && ans[st][yu] != -1) return ans[st][yu];
  bool v[10] = {0};
  long long res = 0;
  for (int i = 1; i <= tot; i++)
    if (!v[a[i]] && !(st & (1 << (i - 1))) && (!flag || a[i]))
      v[a[i]] = 1, res += dfs(st | (1 << (i - 1)), (yu * 10 + a[i]) % m,
                              (flag && a[i] == 0));
  if (!flag) ans[st][yu] = res;
  return res;
}
long long gao(long long x) {
  while (x) a[++tot] = x % 10, x /= 10;
  return dfs(0, 0, 1);
}
int main() {
  memset(ans, -1, sizeof(ans));
  scanf("%I64d%I64d", &n, &m);
  printf("%I64d\n", gao(n));
}
