#include <bits/stdc++.h>
using namespace std;
long long setBit(long long num, long long i) { return num | (1 << i); }
long long unsetBit(long long num, long long i) {
  num ^= 1 << i;
  return num;
}
bool checkBit(long long num, long long i) {
  if (num & 1 << i)
    return true;
  else
    return false;
}
long long bp(long long num) { return __builtin_popcount(num); }
long long giaithua(long long m) {
  if (m == 1) return 1;
  return m * giaithua(m - 1);
}
long long k, n, m, a[20], count1 = 0, dp[300000][105], used[105];
int main() {
  cin >> n >> m;
  k = n;
  memset(used, 0, sizeof(used));
  while (k != 0) {
    a[count1] = k % 10;
    used[a[count1]]++;
    k = k / 10;
    count1++;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < count1; i++) {
    if (a[i] != 0) {
      dp[setBit(0, i)][a[i] % m] = 1;
    } else {
      dp[setBit(0, i)][a[i] % m] = 0;
    }
  }
  int k = 1 << count1;
  for (int i = 0; i < k; i++) {
    for (int ij = 0; ij < m; ij++) {
      if (dp[i][ij]) {
        for (int j = 0; j < count1; j++) {
          if (checkBit(i, j) == 0) {
            dp[setBit(i, j)][(ij * 10 + a[j]) % m] += dp[i][ij];
          }
        }
      }
    }
  }
  for (int i = 0; i <= 9; i++) {
    if (used[i] != 0) dp[k - 1][0] /= giaithua(used[i]);
  }
  cout << dp[k - 1][0];
}
