#include <bits/stdc++.h>
using namespace std;
long long f[1 << 18][100];
int a[100];
int main() {
  long long n, m;
  int N = 0;
  cin >> n >> m;
  for (; n; n /= 10) a[N++] = n % 10;
  sort(a, a + N);
  int mask, k;
  f[0][0] = 1;
  for (mask = 0; mask < (1 << N); mask++)
    for (k = 0; k < m; k++)
      if (f[mask][k]) {
        long long cnt = f[mask][k], pre = -1;
        for (int i = N - 1; i >= 0; i--)
          if (~mask >> i & 1) {
            if (a[i] == pre) continue;
            if (a[i] == 0 && mask == 0) break;
            f[mask | 1 << i][(k * 10 + a[i]) % m] += cnt;
            pre = a[i];
          }
      }
  cout << f[(1 << N) - 1][0] << endl;
  return 0;
}
