#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 2e6 + 10;
vector<int> V[20];
int cnt[N], pos[N], dig[20], num[13];
vector<long long> f[20][105];
int main() {
  for (int i = 1; i <= 2000000; i++) cnt[i] = cnt[i >> 1] + (i & 1);
  long long n, m;
  cin >> n >> m;
  int p = 0;
  long long x = n;
  while (x) dig[p] = x % 10, p++, x /= 10;
  for (int i = 0; i < p; i++) num[dig[i]]++;
  int len = 1 << p;
  for (int i = 0; i < len; i++) {
    V[p - cnt[i]].push_back(i);
  }
  for (int i = 0; i <= p; i++) {
    int siz = V[i].size();
    for (int j = 0; j < m; j++) f[i][j].resize(siz + 1);
    for (int j = 0; j < siz; j++) {
      pos[V[i][j]] = j;
    }
  }
  f[p][0][0] = 1;
  for (int i = p; i >= 1; i--) {
    int siz = V[i].size();
    for (int j = 0; j < siz; j++) {
      for (int k = 0; k < p; k++) {
        if (i == p && dig[k] == 0) continue;
        if (((V[i][j] >> k) & 1) == 0) {
          for (int l = 0; l < m; l++) {
            f[i - 1][(l * 10 + dig[k]) % m][pos[V[i][j] | (1 << k)]] +=
                f[i][l][j];
          }
        }
      }
    }
  }
  long long ans = f[0][0][0];
  for (int i = 0; i < 10; i++) {
    for (int j = 1; j <= num[i]; j++) ans /= j;
  }
  cout << ans;
  return 0;
}
