#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 15;
const int inf = 1e9 + 7;
const long long MOD = 1e9 + 7;
double eps = 1e-9;
long long f[20];
int digit[20];
int a[20];
long long dp[1 << 18][105];
int m;
void init() {
  f[0] = 1;
  for (int i = 1; i <= 18; i++) {
    f[i] = f[i - 1] * i;
  }
  memset(dp, -1, sizeof(dp));
}
long long dfs(int cur, int mod, int len) {
  if (dp[cur][mod] != -1) return dp[cur][mod];
  if (cur == 0) {
    if (mod == 0)
      return 1;
    else
      return 0;
  }
  long long res = 0;
  bool vis[10];
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < len; i++) {
    if (cur & (1 << i)) {
      if (vis[digit[len - i]]) continue;
      vis[digit[len - i]] = true;
      res +=
          dfs(cur ^ (1 << i), ((10 * mod - digit[len - i]) % m + m) % m, len);
    }
  }
  dp[cur][mod] = res;
  return res;
}
int main() {
  init();
  long long n;
  cin >> n >> m;
  int len = 0;
  while (n) {
    len++;
    digit[len] = n % 10;
    a[n % 10]++;
    n /= 10;
  }
  long long ans = 0;
  dp[0][0] = 1;
  bool vis[10];
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < len; i++) {
    if (digit[len - i] && vis[digit[len - i]] == false) {
      vis[digit[len - i]] = true;
      ans += dfs(((1 << len) - 1) ^ (1 << i), ((-digit[len - i]) % m + m) % m,
                 len);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
