#include <bits/stdc++.h>
using namespace std;
long long d[262145][101], n, k, m, sz;
long long po[1005], fc[50], G[50], PP;
string s;
int main() {
  int M = int(1e9) + 7;
  cin >> s >> m;
  fc[0] = 1;
  for (int i = 1; i <= 18; i++) fc[i] = (fc[i - 1] * i);
  n = s.size();
  for (int i = 0; i < n; i++) G[s[i] - '0']++;
  PP = 1;
  for (int i = 0; i < 10; i++) PP *= fc[G[i]];
  d[0][0] = 1;
  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < m; j++) {
      for (int q = 0; q < n; q++) {
        if (!(i & (1 << q)))
          if (i || (s[q] - '0') > 0) {
            d[i | (1 << q)][(j * 10 + s[q] - '0') % m] += d[i][j];
          }
      }
    }
  }
  cout << d[(1 << n) - 1][0] / PP << endl;
  cin >> n;
}
