#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
long long n, x, dp[1 << 18][100];
int m, p[20], len;
long long dfs(int pos, int status, bool zero, int rev) {
  if (!pos) return rev == 0;
  if (!zero && dp[status][rev] != -1) return dp[status][rev];
  long long ans = 0;
  int f[10];
  memset(f, 0, sizeof(f));
  for (int i = 1; i <= len; i++)
    if (!((status >> i - 1) & 1)) {
      if (zero && p[i] == 0) continue;
      if (f[p[i]]) continue;
      ans += dfs(pos - 1, status | (1 << i - 1), zero && p[i] == 0,
                 (rev * 10 + p[i]) % m);
      f[p[i]] = 1;
    }
  if (!zero) dp[status][rev] = ans;
  return ans;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  memset(dp, -1, sizeof(dp));
  cin >> n >> m;
  x = n;
  while (x) {
    p[++len] = x % 10;
    x /= 10;
  }
  long long ans = dfs(len, 0, true, 0);
  cout << ans;
  return 0;
}
