#include <bits/stdc++.h>
using namespace std;
long long N, M, n;
long long digits[100];
long long dp[1 << 18][100];
int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M;
  for (n = 0; N > 0; n++) {
    digits[n] = N % 10;
    N /= 10;
  }
  sort(digits, digits + n, greater<int>());
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (long long i = 0; i < (1 << n); i++) {
    for (long long first = 0; first < M; first++) {
      if (dp[i][first] == 0) continue;
      int prev = -1;
      for (long long j = 0; j < n; j++) {
        if ((i | (1 << j)) == i) continue;
        if (prev == digits[j]) continue;
        if (i == 0 && digits[j] == 0) continue;
        dp[i | (1 << j)][(first * 10 + digits[j]) % M] += dp[i][first];
        prev = digits[j];
      }
    }
  }
  cout << dp[(1 << n) - 1][0];
  return 0;
}
