#include <bits/stdc++.h>
using namespace std;
long long dp[(1 << 18)][111];
int main() {
  int n, m, x, y, i, pre;
  char str[20];
  int a[20], j, k, ii;
  while (cin >> str >> m) {
    n = strlen(str);
    for (i = 0; i < n; i++) a[i] = str[i] - '0';
    sort(a, a + n);
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    y = (1 << n);
    for (i = 0; i < y; i++) {
      for (j = 0; j < m; j++)
        if (dp[i][j]) {
          pre = -1;
          x = j * 10;
          for (k = 0; k < n; k++)
            if (!(i & (1 << k))) {
              if ((i == 0 && a[k] == 0) || pre == a[k]) continue;
              ii = (x + a[k]) % m;
              dp[(1 << k) | i][ii] += dp[i][j];
              pre = a[k];
            }
        }
    }
    printf("%I64d\n", dp[y - 1][0]);
  }
  return 0;
}
