#include <bits/stdc++.h>
using namespace std;
long long dp[262144][101];
queue<int> Q;
int K[262144], P[19], A[19];
char S[20];
int main() {
  int a, b, t, l, n, la, s;
  cin >> S + 1 >> b, dp[0][0] = 1, a = strlen(S + 1), l = (1 << a) - 1,
                     Q.push(0), n = 1, P[0] = 1 % b;
  for (int i = 1; i <= 18; i++) P[i] = P[i - 1] * 10 % b;
  for (int r = 1; r <= a; r++) {
    s = S[r] - '0', t = 0, A[s]++;
    for (int i = 1; i <= n; i++) {
      la = Q.front(), Q.pop();
      for (int j = 0; j < a; j++)
        if (!(la & (1 << j))) {
          if (s == 0 && j == a - 1) continue;
          int nw = la | (1 << j), v = P[j] * s % b;
          for (int k = 0; k < b; k++) dp[nw][(v + k) % b] += dp[la][k];
          if (!K[nw]) K[nw] = 1, Q.push(nw), t++;
        }
    }
    n = t;
  }
  for (int i = 0; i <= 9; i++)
    for (int j = 2; j <= A[i]; j++) dp[l][0] /= j;
  cout << dp[l][0];
}
