from collections import Counter

n,p=list(map(int,input().split()))

s=eval(input())

DP=[0]

ans=0



if p==2 or p==5:

    for i,j in enumerate(s):

        if int(j)%p==0:

            ans +=i+1

else:

    num,point=0,1

    for i in s[::-1]:

        num +=int(i)*point

        num %=p

        DP.append(num)

        point *=10

        point %=p



    DP=Counter(DP)

    for v,m in list(DP.items()):

        if m>=2:ans +=m*(m-1)//2

print(ans)
from collections import Counter

n,p=list(map(int,input().split()))

s=eval(input())



ans=0

if p==2 or p==5:

    for i,j in enumerate(s):

        if int(j)%p==0:

            ans +=i+1

else:

    num,point=0,1

    A=[0]

    for i in s[::-1]:

        num +=int(i)*point

        num %=p

        A.append(num)

        point *=10

        point %=p



    A=Counter(A)

    for i,j in list(A.items()):

        if j>=2:ans +=j*(j-1)//2

print(ans)
from collections import Counter

n, p = list(map(int, input().split()))
s = eval(input())
DP = [0]
ans = 0
if p == 2 or p == 5:
    for i, j in enumerate(s):
        if int(j) % p == 0:
            ans += i + 1
else:
    num, point = 0, 1
    for i in s[::-1]:
        num += int(i) * point
        num %= p
        DP.append(num)
        point *= 10
        point %= p
    DP = Counter(DP)
    for v, m in list(DP.items()):
        if m >= 2:
            ans += m * (m - 1) // 2
print(ans)

from collections import Counter

n, p = list(map(int, input().split()))
s = eval(input())
ans = 0
if p == 2 or p == 5:
    for i, j in enumerate(s):
        if int(j) % p == 0:
            ans += i + 1
else:
    num, point = 0, 1
    A = [0]
    for i in s[::-1]:
        num += int(i) * point
        num %= p
        A.append(num)
        point *= 10
        point %= p
    A = Counter(A)
    for i, j in list(A.items()):
        if j >= 2:
            ans += j * (j - 1) // 2
print(ans)

-DP = [0] +    A = [0] -        DP.append(num) +        A.append(num) -    DP = Counter(DP) -    for v, m in list(DP.items()): -        if m >= 2: -            ans += m * (m - 1) // 2 +    A = Counter(A) +    for i, j in list(A.items()): +        if j >= 2: +            ans += j * (j - 1) // 2