#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int inf = 1061109567;
const int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int digit[20], m, cnt;
long long dp[1 << 18][101];
long long dfs(int s, int num) {
  if (num == 0 && s == 0) return 1;
  if (~dp[s][num]) return dp[s][num];
  int used[10];
  memset(used, 0, sizeof(used));
  long long ret = 0;
  for (int i = 0; i < cnt; i++) {
    if (!(s & (1 << i))) continue;
    if (used[digit[i]]) continue;
    used[digit[i]] = 1;
    ret += dfs(s ^ (1 << i), (num * 10 + digit[i]) % m);
  }
  return dp[s][num] = ret;
}
int main() {
  int num[12];
  memset(num, 0, sizeof(num));
  memset(dp, -1, sizeof(dp));
  long long n;
  cin >> n >> m;
  while (n) {
    digit[cnt++] = n % 10;
    n /= 10;
  }
  long long ans = 0;
  long long s = (1 << cnt) - 1;
  for (int i = 0; i < cnt; i++) {
    if (digit[i]) {
      if (num[digit[i]]) continue;
      num[digit[i]] = 1;
      ans += dfs(s ^ (1 << i), digit[i] % m);
    }
  }
  cout << ans << endl;
  return 0;
}
