#include <bits/stdc++.h>
using namespace std;
map<pair<vector<int>, int>, long long> ma;
long long dp[(1 << 18)][101];
int main() {
  srand((unsigned int)time(NULL));
  long long n, m;
  int dig[20], id = 0, ten[20], cnt[10] = {};
  ten[0] = 1;
  cin >> n >> m;
  for (int i = 1; i <= 19; i++) ten[i] = (ten[i - 1] * 10) % m;
  while (n) {
    cnt[n % 10]++;
    dig[id++] = n % 10;
    n /= 10;
  }
  dp[0][0] = 1LL;
  for (int mask = 0; mask < (1 << id); mask++) {
    for (int i = 0; i < id; i++) {
      if (!mask && !dig[i]) continue;
      if (((mask >> i) & 1)) continue;
      for (int rem = 0; rem < m; rem++) {
        if (!dp[mask][rem]) continue;
        dp[mask ^ (1 << i)][(rem * 10 + dig[i]) % m] += dp[mask][rem];
      }
    }
  }
  for (int i = 0; i <= 9; i++)
    for (int j = 1; j <= cnt[i]; j++) dp[(1 << id) - 1][0] /= j;
  cout << dp[(1 << id) - 1][0] << endl;
}
