#include <bits/stdc++.h>
using namespace std;
bool chk(int msk, int ps) { return (bool)(msk & (1 << ps)); }
int seti(int msk, int ps) { return (msk |= (1 << ps)); }
string str;
int m;
long long int dp[(1 << 18) + 2][101];
vector<int> vt[13];
long long int rec(int msk, int num) {
  if (__builtin_popcount(msk) == str.size()) {
    if (num == 0)
      return 1;
    else
      return 0;
  }
  long long int &ret = dp[msk][num];
  if (ret != -1) return ret;
  ret = 0;
  for (int d = 0; d < 10; d++) {
    if (msk == 0 and d == 0) continue;
    for (int x : vt[d]) {
      if (chk(msk, x) == 0) {
        ret += rec(seti(msk, x), (num * 10 + d) % m);
        break;
      }
    }
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> str;
  for (int i = 0; i < str.size(); i++) {
    char x = str[i];
    vt[x - '0'].push_back(i);
  }
  cin >> m;
  memset(dp, -1, sizeof dp);
  long long int ans = rec(0, 0);
  cout << ans << endl;
  return 0;
}
