from collections.abc import Iterable def accum_dp(xs: Iterable, f, op, e, init: dict, *, is_reset=True): dp = init.copy() for x in xs: pp = {} if is_reset else dp.copy() dp, pp = pp, dp for fm_key, fm_val in pp.items(): for to_key, to_val in f(fm_key, fm_val, x): dp[to_key] = op(dp.get(to_key, e), to_val) return dp def f(k, v, x): global ans b, m = k # (開始ノードか, mod) if b: if x > 0: if x % M == 0: ans += 1 yield (False, x % M), v else: nm = (10*m + x) % M if nm == 0: ans += v yield (False, nm), v def op(a, b): return (a + b) % MOD def digit_dp(): digits = [int(c) for c in S] init = {(True, 0): 1} _ = accum_dp(digits, f, op, 0, init, is_reset=False) MOD = 10**9 + 7 S = input() M = int(input()) ans = 0 digit_dp() ans += S.count('0') print(ans)