#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string s;
  int m;
  cin >> s >> m;
  int n = s.size();
  int N = 1 << n;
  long long int dp[N][m];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < m; ++j) {
      if (dp[i][j]) {
        for (int k = 0; k < n; ++k) {
          if (!(i & (1 << k))) {
            if (i || (s[k] - '0')) {
              dp[i | (1 << k)][(j * 10 + (s[k] - '0')) % m] += dp[i][j];
            }
          }
        }
      }
    }
  }
  long long int ans = dp[N - 1][0];
  int arr[10] = {0};
  for (int i = 0; i < n; ++i) {
    arr[s[i] - '0']++;
  }
  long long int facts[19];
  facts[0] = 1;
  for (int i = 1; i < 19; ++i) {
    facts[i] = i * facts[i - 1];
  }
  for (int i = 0; i < 10; ++i) {
    ans /= facts[arr[i]];
  }
  cout << ans << '\n';
}
