#include <bits/stdc++.h>
using namespace std;
using namespace std;
long long f[(1 << 18)][100];
int a[20];
bool vis[10];
int main() {
  long long n;
  int m;
  while (cin >> n >> m) {
    memset(f, 0, sizeof f);
    int tot(0);
    while (n) {
      a[++tot] = n % 10;
      n /= 10;
    }
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= tot; ++i)
      if (a[i] && !vis[a[i]]) {
        vis[a[i]] = 1;
        f[1 << (i - 1)][a[i] % m] = 1;
      }
    for (int k = 1; k <= (1 << tot) - 2; ++k) {
      memset(vis, 0, sizeof vis);
      for (int i = 1; i <= tot; ++i)
        if ((k & (1 << (i - 1))) == 0)
          if (!vis[a[i]]) {
            vis[a[i]] = 1;
            for (int j = 0; j <= m - 1; ++j)
              f[(1 << (i - 1)) | k][(j * 10 + a[i]) % m] += f[k][j];
          }
    }
    cout << f[(1 << tot) - 1][0] << endl;
  }
  return 0;
}
