#include <bits/stdc++.h>
using namespace std;
long long dp[1 << 18][100];
int co[1 << 18];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  long long m;
  cin >> m;
  long long n = s.size();
  long long c[10];
  memset(c, 0, sizeof c);
  for (long long i = 0; i < n; i++) c[s[i] - '0']++;
  vector<long long> v[1 << n];
  for (long long i = 0; i < (1 << n); i++) {
    int r = 0;
    for (long long j = 0; j < n; j++) {
      if (i & (1 << j)) {
        r++;
      } else
        v[i].push_back(j);
    }
    co[i] = r;
  }
  dp[(1 << n) - 1][0] = 1;
  long long pw[20];
  pw[0] = 1 % m;
  int e[20];
  int r = -1;
  for (int i = 0; i < n; i++) {
    int r = 0;
    for (int j = 0; j <= 9; j++) {
      r += c[j];
      if (i < r) {
        e[i] = j;
        break;
      }
    }
  }
  for (long long i = 1; i < 20; i++) {
    pw[i] = (pw[i - 1] * 10) % m;
  }
  for (long long mask = (1 << n) - 1; mask >= 0; mask--) {
    for (long long mod = 0; mod < m; mod++) {
      int now = e[co[mask]];
      for (int j = 0; j < v[mask].size(); j++) {
        if (now == 0 && v[mask][j] == n - 1) continue;
        int notun = (mod + pw[v[mask][j]] * now) % m;
        dp[mask][mod] += dp[mask | (1 << v[mask][j])][notun];
      }
    }
  }
  for (long long i = 0; i < 10; i++) {
    long long r = 1;
    for (long long j = 1; j <= c[i]; j++) r *= j;
    dp[0][0] /= r;
  }
  cout << dp[0][0];
}
