#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
long long n;
int m;
bool used[10];
int bits[1 << 18];
long long base[18];
long long f[1 << 18][100];
int main() {
  cin >> n >> m;
  base[0] = 1;
  for (int i = 1; i < 18; ++i) base[i] = base[i - 1] * 10;
  bits[0] = 0;
  for (int i = 1; i < (1 << 18); ++i) bits[i] = bits[i & (i - 1)] + 1;
  for (int i = 0; i < (1 << 18); ++i)
    for (int j = 0; j < 100; ++j) f[i][j] = 0;
  int N = 0;
  for (long long x = n; x > 0; x /= 10) ++N;
  f[0][0] = 1;
  for (int i = 1; i < (1 << N); ++i) {
    for (int k = 0; k < 10; ++k) used[k] = false;
    for (int k = 0; k < N; ++k) {
      if (i & (1 << k)) {
        int d = n / base[k] % 10;
        if (used[d]) continue;
        if (i == (1 << N) - 1 and d == 0) continue;
        for (int j = 0; j < m; ++j)
          f[i][(j + d * base[bits[i] - 1]) % m] += f[i ^ (1 << k)][j];
        used[d] = true;
      }
    }
  }
  cout << f[(1 << N) - 1][0] << endl;
  return 0;
}
