#include <bits/stdc++.h>
using namespace std;
long long dp[(1 << 18)][101], n, m;
long long v[22], gigi;
string str;
int main() {
  ios::sync_with_stdio(false);
  cin >> str >> m;
  n = str.size();
  for (int i = str.size() - 1; i >= 0; i--) {
    if (i == str.size() - 1)
      v[i] = 1;
    else
      v[i] = v[i + 1] * 10;
  }
  dp[0][0] = 1;
  for (int mask = 0; mask <= (1 << n) - 1; mask++) {
    for (int r = 0; r <= m - 1; r++) {
      int can[10], p = 0;
      for (int i = 0; i <= 9; i++) can[i] = -1;
      for (int i = 0; i <= n - 1; i++) {
        if (mask & (1 << i)) {
          p++;
          continue;
        }
        int c = str[i] - '0';
        can[c] = i;
      }
      for (int i = 0; i <= 9; i++) {
        if (!mask && !i) continue;
        if (can[i] == -1) continue;
        long long newMask = mask | (1 << can[i]), nr = (r + (i * v[p])) % m;
        dp[newMask][nr] += dp[mask][r];
      }
    }
  }
  cout << dp[(1 << n) - 1][0] << endl;
  return 0;
}
