#include <bits/stdc++.h>
using namespace std;
long long dp[1 << 18][111];
int cnt[11];
int a[11];
string s;
int m;
int main() {
  cin >> s >> m;
  int l = s.size();
  memset(cnt, 0, sizeof(cnt));
  memset(a, 0, sizeof(a));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < l; i++) cnt[s[i] - '0']++;
  for (int(i) = (1); (i) < (11); (i)++) a[i] = a[i - 1] + cnt[i - 1];
  for (int(i) = (1); (i) < (10); (i)++)
    if (cnt[i]) dp[1 << a[i]][i % m] = 1;
  for (int(i) = (1); (i) < ((1 << l)); (i)++)
    for (int(j) = (0); (j) < (m); (j)++)
      if (dp[i][j])
        for (int(k) = (0); (k) < (10); (k)++)
          for (int(p) = (a[k]); (p) < (a[k + 1]); (p)++) {
            if (i & (1 << p)) continue;
            dp[i + (1 << p)][(j * 10 + k) % m] += dp[i][j];
            break;
          }
  cout << dp[(1 << l) - 1][0] << endl;
  return 0;
}
