#include <bits/stdc++.h>
using namespace std;
long long lp, numb[20], u;
long long dp[300000][105];
long long dmc(long long msk, long long mod) {
  if (msk + 1 == (1 << u) && !(mod % lp)) return 1;
  if (msk + 1 == (1 << u)) return 0;
  if (dp[msk][mod] != -1) return dp[msk][mod];
  long long i, j, res = 0, ty = 0;
  for (i = 0; i < u; i++) {
    if ((ty & (1 << numb[i]))) continue;
    if (!msk && !numb[i]) continue;
    if ((msk & (1 << i))) continue;
    res += dmc(msk | (1 << i), (mod * 10 + numb[i]) % lp);
    ty |= (1 << numb[i]);
  }
  return dp[msk][mod] = res;
}
int main() {
  long long i, j, k;
  string temp;
  cin >> temp >> lp;
  u = temp.size();
  for (i = 0; i < u; i++) numb[i] = temp[i] - '0';
  sort(numb, numb + u);
  memset(dp, -1, sizeof dp);
  cout << dmc(0, 0);
  return 0;
}
