#include <bits/stdc++.h>
using namespace std;
string s;
int n, m;
long long dp[1 << 18][105];
long long power(int x, int p) {
  if (p == 0) return 1;
  return x * power(x, p - 1);
}
long long getCount(int mask, int mod) {
  if (mask == (1 << n) - 1)
    if (mod == 0)
      return 1;
    else
      return 0;
  long long &ret = dp[mask][mod];
  if (ret != -1) return ret;
  ret = 0;
  int f[10] = {0};
  for (int i = 0; i < n; i++) {
    if (mask == 0 && s[i] == '0') continue;
    if (((mask >> i) & 1) == 0 && !f[s[i] - '0'])
      ret += getCount(mask | (1 << i), (mod * 10 + s[i] - '0') % m),
          f[s[i] - '0'] = 1;
  }
  return ret;
}
int main() {
  cin >> s >> m;
  n = s.length();
  memset(dp, -1, sizeof dp);
  cout << getCount(0, 0) << endl;
  return 0;
}
