#include <bits/stdc++.h>
using namespace std;
long long n, k, i, f[100], j, ii, dp[(1LL << 18)][101], s[11], d[20];
char ss[100];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> ss >> k;
  n = strlen(ss);
  for (i = 0; i < n; i++) f[i] = ss[i] - '0', s[f[i]]++;
  sort(f, f + n);
  d[0] = 1;
  for (i = 1; i < 19; i++) d[i] = d[i - 1] * i;
  dp[0][0] = 1;
  for (i = 0; i < (1LL << n); i++)
    for (j = 0; j < k; j++) {
      if (dp[i][j] == 0) continue;
      for (ii = 0; ii < n; ii++)
        if (((1LL << ii) & i) == 0) {
          if (f[ii] == 0 && i == 0)
            continue;
          else
            dp[(i | (1LL << ii))][(j * 10 + f[ii]) % k] += dp[i][j];
        }
    }
  for (i = 0; i < 10; i++) dp[(1LL << n) - 1][0] /= d[s[i]];
  cout << dp[(1LL << n) - 1][0];
}
