#include #include #define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++) using namespace std; typedef __int64_t ll; const ll mod = 1e9 + 7; int main(void){ string s; int m; cin >> s >> m; int len = s.length(); if (len >= 20) { return 1; // RE } int cnt = 0; REP (bits, 1, 1 << len) { ll sum = 0; int zero = 0; REP (i, 0, len) { if (bits & (1 << i)) { sum *= 10; sum += s[i] - '0'; sum %= m; if (zero == 0 && s[i] == '0' && bits != (1 << i)) { zero = 1; } else { zero = 2; } } } cnt += sum == 0 && zero != 1; } cout << cnt << endl; }