#include <bits/stdc++.h>
using namespace std;
long long dp[(1 << 20) + 3][20];
int digit[20];
bool flag[10];
int main() {
  long long q;
  long long m;
  scanf("%lld%lld", &q, &m);
  int n = 0;
  while (q) {
    n++;
    digit[n] = q % 10;
    q /= 10;
  }
  for (int i = 1; i <= n / 2; i++) swap(digit[i], digit[n - i + 1]);
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < 1 << n; i++) {
    for (int j = 0; j < m; j++) {
      if (dp[i][j] == 0) continue;
      memset(flag, 0, sizeof(flag));
      for (int k = 1; k <= n; k++) {
        if (!flag[digit[k]]) {
          if (i & (1 << (k - 1))) continue;
          if (i == 0 && digit[k] == 0) continue;
          flag[digit[k]] = 1;
          dp[i | (1 << (k - 1))][(j * 10 + digit[k]) % m] += dp[i][j];
        }
      }
    }
  }
  printf("%lld", dp[(1 << n) - 1][0]);
  return 0;
}
