#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const long long infLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 18 + 5;
const int maxs = (1 << 18) + 5;
const int maxm = 100 + 5;
string s;
int n, m;
long long f[maxs][maxm];
int cnt[maxn];
long long fac[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin >> s >> m;
  n = s.length();
  for (int i = 0; i < (n); ++i) ++cnt[s[i] - '0'];
  fac[0] = 1;
  for (int i = 1; i < maxn; ++i) fac[i] = fac[i - 1] * i;
  f[0][0] = 1;
  for (int S = 0; S < ((1 << n)); ++S)
    for (int j = 0; j < (m); ++j)
      if (f[S][j]) {
        for (int i = 0; i < (n); ++i)
          if (~S & (1 << i)) {
            if (!S && s[i] == '0') continue;
            f[S | (1 << i)][(j * 10 + s[i] - '0') % m] += f[S][j];
          }
      }
  long long res = f[(1 << n) - 1][0];
  for (int i = 0; i < (10); ++i) res /= fac[cnt[i]];
  cout << res << endl;
  return 0;
}
