I = lambda s:list(map(int, s))



p = int(input().split()[1])

s = eval(input())

if 10 % p == 0:

  r = sum(i for i, x in enumerate(I(s), 1) if x % p == 0)

else:

  d = {0 : 1}

  t, y = 0, 1

  for i, x in enumerate(I(reversed(s)), 1):

    t = (t + x * y) % p

    d[t] = d.get(t, 0) + 1

    y = y * 10 % p;

  r = sum(i * (i - 1) // 2 for i in list(d.values()))

print(r)

p = int(input().split()[1])

a = list(map(int, eval(input())))

if 10 % p == 0:

  r = sum(i for i, x in enumerate(a, 1) if x % p == 0)

else:

  d = [1] + [0] * (p - 1)

  t, y = 0, 1

  for i, x in enumerate(reversed(a), 1):

    t = (t + x * y) % p

    d[t] += 1

    y = y * 10 % p;

  r = sum(i * (i - 1) // 2 for i in d)

print(r)

I = lambda s: list(map(int, s))
p = int(input().split()[1])
s = eval(input())
if 10 % p == 0:
    r = sum(i for i, x in enumerate(I(s), 1) if x % p == 0)
else:
    d = {0: 1}
    t, y = 0, 1
    for i, x in enumerate(I(reversed(s)), 1):
        t = (t + x * y) % p
        d[t] = d.get(t, 0) + 1
        y = y * 10 % p
    r = sum(i * (i - 1) // 2 for i in list(d.values()))
print(r)

p = int(input().split()[1])
a = list(map(int, eval(input())))
if 10 % p == 0:
    r = sum(i for i, x in enumerate(a, 1) if x % p == 0)
else:
    d = [1] + [0] * (p - 1)
    t, y = 0, 1
    for i, x in enumerate(reversed(a), 1):
        t = (t + x * y) % p
        d[t] += 1
        y = y * 10 % p
    r = sum(i * (i - 1) // 2 for i in d)
print(r)

-I = lambda s: list(map(int, s)) -s = eval(input()) +a = list(map(int, eval(input()))) -    r = sum(i for i, x in enumerate(I(s), 1) if x % p == 0) +    r = sum(i for i, x in enumerate(a, 1) if x % p == 0) -    d = {0: 1} +    d = [1] + [0] * (p - 1) -    for i, x in enumerate(I(reversed(s)), 1): +    for i, x in enumerate(reversed(a), 1): -        d[t] = d.get(t, 0) + 1 +        d[t] += 1 -    r = sum(i * (i - 1) // 2 for i in list(d.values())) +    r = sum(i * (i - 1) // 2 for i in d)