#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &a) {
  register int b = 1, c = getchar();
  a = 0;
  for (; !isdigit(c); c = getchar())
    if (c == 45) b *= -1;
  for (; isdigit(c); c = getchar()) a = (a << 3) + (a << 1) + c - 48;
  a *= b;
}
const int maxn = 105;
const int maxm = 1 << 18;
long long dp[maxm][maxn];
string str;
int len;
int m;
int main() {
  cin >> str;
  len = int((str).size());
  read(m);
  dp[0][0] = 1;
  for (int msk = 0; msk < int(1 << len); msk++) {
    for (int rem = 0; rem < int(m); rem++) {
      for (int i = 0; i < int(10); i++) {
        for (int j = 0; j < int(len); j++)
          if (str[j] == i + 48 && !(msk & (1 << j))) {
            int nwrem = (rem * 10 + i) % m;
            int nwmsk = msk | (1 << j);
            if (!msk && i == 0) continue;
            dp[nwmsk][nwrem] += dp[msk][rem];
            break;
          }
      }
    }
  }
  printf("%I64d\n", dp[(1 << len) - 1][0]);
  return 0;
}
