import sys

input = sys.stdin.readline

from collections import Counter



N, mod = list(map(int, input().split()))

S = list(input().rstrip())



if mod == 2 or mod == 5:

    ans = 0

    for i, s in enumerate(S):

        if int(s)%mod==0:

            ans += i+1

else:

    dp = [0]

    t = 1

    for s in reversed(S):

        dp.append((dp[-1]+int(s)*t)%mod)

        t = t*10%mod



    ans = 0

    C = Counter(dp)

    for v in list(C.values()):

        ans += v*(v-1)//2



print(ans)
import sys

input = sys.stdin.readline



N, P = list(map(int, input().split()))

S = list(input().rstrip())



if P in {2, 5}:

    ans = 0

    for i, s in enumerate(S):

        if int(s) % P == 0:

            ans += i+1

else:

    ans = 0

    T = [0]*P

    T[0] = 1

    tmp = 0

    k = 1

    for s in reversed(S):

        tmp = (tmp + k * int(s)) % P

        k = k * 10 % P

        ans += T[tmp]

        T[tmp] += 1



print(ans)
import sys

input = sys.stdin.readline
from collections import Counter

N, mod = list(map(int, input().split()))
S = list(input().rstrip())
if mod == 2 or mod == 5:
    ans = 0
    for i, s in enumerate(S):
        if int(s) % mod == 0:
            ans += i + 1
else:
    dp = [0]
    t = 1
    for s in reversed(S):
        dp.append((dp[-1] + int(s) * t) % mod)
        t = t * 10 % mod
    ans = 0
    C = Counter(dp)
    for v in list(C.values()):
        ans += v * (v - 1) // 2
print(ans)

import sys

input = sys.stdin.readline
N, P = list(map(int, input().split()))
S = list(input().rstrip())
if P in {2, 5}:
    ans = 0
    for i, s in enumerate(S):
        if int(s) % P == 0:
            ans += i + 1
else:
    ans = 0
    T = [0] * P
    T[0] = 1
    tmp = 0
    k = 1
    for s in reversed(S):
        tmp = (tmp + k * int(s)) % P
        k = k * 10 % P
        ans += T[tmp]
        T[tmp] += 1
print(ans)

-from collections import Counter - -N, mod = list(map(int, input().split())) +N, P = list(map(int, input().split())) -if mod == 2 or mod == 5: +if P in {2, 5}: -        if int(s) % mod == 0: +        if int(s) % P == 0: -    dp = [0] -    t = 1 +    ans = 0 +    T = [0] * P +    T[0] = 1 +    tmp = 0 +    k = 1 -        dp.append((dp[-1] + int(s) * t) % mod) -        t = t * 10 % mod -    ans = 0 -    C = Counter(dp) -    for v in list(C.values()): -        ans += v * (v - 1) // 2 +        tmp = (tmp + k * int(s)) % P +        k = k * 10 % P +        ans += T[tmp] +        T[tmp] += 1