#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  int m;
  cin >> m;
  int n = s.size();
  sort(s.begin(), s.end());
  int limit = (1 << n);
  long long int dp[limit][m];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < limit; i++) {
    for (int j = 0; j < m; j++) {
      if (!dp[i][j]) continue;
      char prev = 'X';
      for (int k = 0; k < n; k++) {
        if (i == 0 && s[k] == '0') continue;
        if (i & (1 << k)) continue;
        if (s[k] == prev) continue;
        prev = s[k];
        int newmod = (j * 10 + (int)(s[k] - '0')) % m;
        int temp = i | (1 << k);
        dp[i | (1 << k)][newmod] += dp[i][j];
      }
    }
  }
  cout << dp[limit - 1][0] << "\n";
  return 0;
}
