#include <bits/stdc++.h>
using namespace std;
long long v;
int n, m;
long long dp[1 << 18][100];
int num[100], cnt[100];
int N;
void solve() {
  N = 1 << n;
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    if (num[i] > 0) dp[1 << i][num[i] % m] += 1;
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < m; j++) {
      if (dp[i][j] == 0) continue;
      for (int k = 0; k < n; k++) {
        if ((i & (1 << k)) > 0) continue;
        dp[i | (1 << k)][(j * 10 + num[k]) % m] += dp[i][j];
      }
    }
  }
  long long ans = dp[N - 1][0];
  for (int i = 0; i <= 9; i++) {
    for (int j = 1; j <= cnt[i]; j++) {
      ans /= j;
    }
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> v >> m;
  n = 0;
  while (v) {
    num[n++] = v % 10;
    cnt[v % 10]++;
    v /= 10;
  }
  solve();
  return 0;
}
