#include <bits/stdc++.h>
using namespace std;
string S;
int MOD;
int len;
long long dp[1 << 18][100];
int cnt[1 << 18];
int bits[1 << 18];
int DMASKS[1 << 18];
int powers[20];
int orig;
long long solve(int mask, int mod) {
  if (mask == 0) {
    return mod == 0;
  }
  long long &ret = dp[mask][mod];
  if (ret != -1) return ret;
  ret = 0;
  int pos = len - cnt[mask];
  int dMask = pos == 0;
  int m = mask;
  while (m > 0) {
    int b = m & -m;
    if (!(dMask & (1 << (S[bits[b]] - '0')))) {
      ret += solve(mask ^ b,
                   (mod + powers[len - pos - 1] * (S[bits[b]] - '0')) % MOD);
      dMask |= 1 << (S[bits[b]] - '0');
    }
    m -= b;
  }
  return ret;
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> S >> MOD;
  len = S.size();
  powers[0] = 1;
  powers[0] %= MOD;
  for (int i = 1; i < 20; i++) {
    powers[i] = powers[i - 1] * 10;
    powers[i] %= MOD;
  }
  for (int i = 0; i < 18; i++) {
    bits[1 << i] = i;
  }
  for (int i = 0, to = 1 << 18; i < to; i++) {
    cnt[i] = __builtin_popcount(i);
  }
  orig = (1 << len) - 1;
  cout << solve(orig, 0) << endl;
  return 0;
}
