#include <bits/stdc++.h>
using namespace std;
int n, m;
int digit[105];
long long Num;
long long dp[1 << 18][105];
int cnt[10];
int main() {
  scanf("%lld%d", &Num, &m);
  while (Num) {
    digit[++n] = Num % 10;
    cnt[Num % 10]++;
    Num /= 10;
  }
  dp[0][0] = 1;
  for (int i = (0), i_end_ = ((1 << n) - 1); i <= i_end_; i++) {
    for (int j = (0), j_end_ = (m - 1); j <= j_end_; j++) {
      if (!dp[i][j]) continue;
      for (int k = (1), k_end_ = (n); k <= k_end_; k++) {
        if (i & (1 << (k - 1))) continue;
        if (!i && !digit[k]) continue;
        int now = i | (1 << (k - 1));
        dp[now][(j * 10 + digit[k]) % m] += dp[i][j];
      }
    }
  }
  long long res = dp[(1 << n) - 1][0];
  for (int i = (0), i_end_ = (9); i <= i_end_; i++) {
    if (cnt[i] >= 2) {
      for (int j = (1), j_end_ = (cnt[i]); j <= j_end_; j++) res /= j;
    }
  }
  printf("%lld\n", res);
  return 0;
}
