#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int num = 0;
  char c = getchar();
  if (c == '-') return -read();
  while (c >= '0' && c <= '9') {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num;
}
inline long long READ() {
  long long num = 0;
  char c = getchar();
  if (c == '-') return -READ();
  while (c >= '0' && c <= '9') {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num;
}
long long n;
int m;
int cnt[10];
long long p10[19], p10m[19];
int wei;
unordered_map<long long, long long> mm;
long long dfs(long long djg, long long num, long long md) {
  if (!djg) return md == 0;
  long long xz = num * 100 + md, bb = num, ans = 0;
  if (mm.count(xz)) return mm[xz];
  for (int i = 0; i < 10; i++) {
    if (djg == wei && i == 0) {
      bb /= 10;
      continue;
    }
    if (bb % 10 < cnt[i]) {
      long long xxx = (md - i * p10[djg - 1]) % m;
      while (xxx < 0) xxx += m;
      ans += dfs(djg - 1, num + p10m[i], xxx);
    }
    bb /= 10;
  }
  return mm[xz] = ans;
}
int main() {
  n = READ();
  m = read();
  p10[0] = p10m[0] = 1;
  for (int i = 1; i < 19; i++) {
    p10[i] = p10[i - 1] * 10 % m;
    p10m[i] = p10m[i - 1] * 10;
  }
  while (n) {
    cnt[n % 10]++;
    wei++;
    n /= 10;
  }
  cout << dfs(wei, 0, 0) << endl;
  return 0;
}
