#include <bits/stdc++.h>
using namespace std;
const int MAX = 18;
long long n;
int m;
int digs;
int dig[MAX];
int cnt[MAX];
long long dp[1 << MAX][105];
long long fact[MAX];
int main() {
  cin >> n >> m;
  fact[0] = 1;
  for (int i = 1; i < MAX; i++) {
    fact[i] = fact[i - 1] * i;
  }
  while (n > 0) {
    dig[digs] = n % 10;
    cnt[n % 10]++;
    digs++;
    n /= 10;
  }
  dp[0][0] = 1;
  for (int i = 0; i < (1 << digs) - 1; i++) {
    for (int mod = 0; mod < m; mod++) {
      if (!dp[i][mod]) continue;
      for (int j = 0; j < digs; j++) {
        if (i & (1 << j) || (dig[j] == 0 && i == 0)) continue;
        dp[i | (1 << j)][(10 * mod + dig[j]) % m] += dp[i][mod];
      }
    }
  }
  long long ans = dp[(1 << digs) - 1][0];
  for (int i = 0; i < MAX; i++) {
    ans /= fact[cnt[i]];
  }
  cout << ans << endl;
}
