#include <bits/stdc++.h>
using namespace std;
bool Check(long long n, long long pos) { return (n & (1 << pos)); }
long long Set(long long n, long long pos) { return (n | (1 << pos)); }
string s;
long long n, mod;
long long dp[(1 << 18) + 2][102];
long long call(long long mask, long long val) {
  if (mask == (1 << n) - 1) {
    if (val)
      return 0;
    else
      return 1;
  }
  long long &ret = dp[mask][val];
  if (ret != -1) return ret;
  ret = 0;
  unordered_map<int, bool> mp;
  for (int i = 0; i < n; i++) {
    int digit = s[i] - '0';
    if (mp[digit] || (!digit && !mask) || Check(mask, i)) continue;
    mp[digit] = 1;
    ret += call(Set(mask, i), (val * 10 + digit) % mod);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s >> mod;
  n = s.size();
  memset(dp, -1, sizeof dp);
  cout << call(0, 0) << endl;
}
