#include <bits/stdc++.h>
using namespace std;
const int maxn = 18, maxm = 100, maxl = 1 << 18;
const long long powten[maxn + 5] = {1,
                                    10,
                                    100,
                                    1000,
                                    10000,
                                    100000,
                                    1000000,
                                    10000000,
                                    100000000,
                                    1000000000,
                                    10000000000,
                                    100000000000,
                                    1000000000000,
                                    10000000000000,
                                    100000000000000,
                                    1000000000000000,
                                    10000000000000000,
                                    100000000000000000};
vector<int> sta[maxn + 5];
int dig[maxn + 5];
long long f[maxl + 5][maxm + 5];
bool rep[maxn + 5];
int main() {
  long long n;
  int m;
  scanf("%lld%d", &n, &m);
  int cnt = 0;
  while (n) {
    dig[cnt++] = n % 10ll;
    n /= 10ll;
  }
  sort(dig, dig + cnt);
  for (int i = 1; i < cnt; i++)
    if (dig[i] == dig[i - 1]) rep[i] = true;
  for (int i = 1; i < 1 << cnt; i++) sta[__builtin_popcount(i)].push_back(i);
  f[0][0] = 1;
  for (int i = 1; i <= cnt; i++) {
    for (int j = 0; j < sta[i].size(); j++) {
      int now = sta[i][j];
      for (int k = 0; k < cnt; k++) {
        if ((now & (1 << k)) == 0) continue;
        if (rep[k] && (now & (1 << (k - 1))) == 0) continue;
        if (dig[k] == 0 && i == cnt) continue;
        int lst = now ^ (1 << k);
        for (int l = 0; l < m; l++)
          f[now][(dig[k] * powten[i - 1] + l) % m] += f[lst][l];
      }
    }
  }
  printf("%lld", f[(1 << cnt) - 1][0]);
}
