#include <bits/stdc++.h>
using namespace std;
long long n;
int m;
long long dp[1 << 18][101];
int num[20];
int c[10];
int digitLen;
long long Fac(int x) { return x == 0 ? 1 : Fac(x - 1) * x; }
int main() {
  cin >> n >> m;
  while (n) {
    int t = (int)(n % 10);
    num[digitLen++] = t;
    c[t]++;
    n /= 10;
  }
  long long div = 1;
  for (int i = 0; i < 10; ++i) div *= Fac(c[i]);
  dp[0][0] = 1;
  for (int i = 0; i < (1 << digitLen); ++i) {
    for (int j = 0; j < m; ++j) {
      if (dp[i][j] == 0) continue;
      for (int k = 0; k < digitLen; ++k) {
        if (1 & (i >> k)) continue;
        if (i || num[k]) {
          int mask = i | (1 << k), mod = (j * 10 + num[k]) % m;
          dp[i | (1 << k)][(j * 10 + num[k]) % m] += dp[i][j];
        }
      }
    }
  }
  printf("%I64d\n", dp[(1 << digitLen) - 1][0] / div);
  return 0;
}
