#include <bits/stdc++.h>
using namespace std;
long long n, cnt, a[20], ten[20];
long long m, b[20], sum;
long long f[101][1 << 18];
long long dfs(int k, long long s, long long num, bool bo) {
  if (f[num][s] != -1) return f[num][s];
  if (k == cnt) return num == 0;
  long long ans = 0;
  for (int i = 0; i < cnt; i++)
    if (!bo || (bo && a[i]))
      if (!((1 << i) & s))
        ans = (ans + dfs(k + 1, s | (1 << i), (num * 10LL + a[i]) % m, 0));
  return f[num][s] = ans;
}
int main() {
  memset(f, -1, sizeof(f));
  cin >> n >> m;
  while (n) {
    a[cnt++] = n % 10;
    b[n % 10]++;
    n /= 10;
  }
  sum = 1;
  for (int i = 0; i < 10; i++)
    for (int j = 2; j <= b[i]; j++) sum *= j;
  sort(a, a + cnt);
  cout << dfs(0, 0, 0, 1) / sum << endl;
  return 0;
}
