#include <bits/stdc++.h>
using namespace std;
const int N = 18;
long long dp[(1 << N)][105];
string n;
int qwq;
int m, pw[20];
bool used[10];
void init() {
  pw[0] = 1;
  for (int i = 1; i <= n.size(); i++) pw[i] = pw[i - 1] * 10 % m;
}
int main() {
  cin >> n >> m;
  dp[0][0] = 1;
  init();
  for (int i = 1; i < (1 << n.size()); i++) {
    int sz = n.size() - __builtin_popcount(i);
    for (int k = 0; k < m; k++) {
      for (int j = 0; j < 10; j++) used[j] = false;
      for (int j = 0; j < n.size(); j++)
        if (((i >> j) & 1) && !used[n[j] - '0'])
          if (sz != (int)n.size() - 1 || n[j] != '0')
            dp[i][k] +=
                dp[i - (1 << j)][(m + k - (n[j] - '0') * pw[sz] % m) % m],
                used[n[j] - '0'] = true;
    }
  }
  cout << dp[(1 << n.size()) - 1][0] << "\n";
  return 0;
}
