#include <bits/stdc++.h>
using namespace std;
vector<int> p, g;
long long n, f[2][27000][105];
int m, cnt[10];
int toInt(vector<int>& state) {
  int res = 0;
  for (int i = 0; i < int(p.size()); i++) {
    if (state[i] == 0) continue;
    res += state[i] * g[i + 1];
  }
  return res;
}
vector<int> toState(int s) {
  vector<int> res(int(p.size()), 0);
  for (int i = 0; i < int(p.size()); i++) {
    res[i] = s / g[i + 1];
    s %= g[i + 1];
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  long long _n = n, digits = 0;
  while (_n) {
    cnt[_n % 10]++;
    _n /= 10;
    digits++;
  }
  for (int i = 0; i < 10; i++) {
    if (cnt[i] == 0) continue;
    p.push_back(i);
  }
  g.resize(int(p.size()) + 1, 0);
  g[int(g.size()) - 1] = 1;
  for (int i = int(g.size()) - 2; i >= 0; i--) {
    g[i] = g[i + 1] * (cnt[p[i]] + 1);
  }
  int final = g[0] - 1;
  f[0][0][0] = 1;
  for (int i = 0; i <= digits - 1; i++) {
    int cur = i & 1, ne = 1 - cur;
    memset(f[ne], 0, sizeof f[ne]);
    for (int j = 0; j < 27000; j++)
      for (int k = 0; k < m; k++) {
        if (f[cur][j][k] == 0) continue;
        vector<int> state = toState(j);
        for (int x = 0; x < int(p.size()); x++) {
          int num = p[x];
          if (i == 0 && num == 0) continue;
          if (cnt[num] == state[x]) continue;
          state[x]++;
          int nextJ = toInt(state);
          state[x]--;
          int nextK = (k * 10 + num) % m;
          f[ne][nextJ][nextK] += f[cur][j][k];
        }
      }
  }
  cout << f[digits & 1][final][0] << "\n";
  return 0;
}
