def main():

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

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

    now = 1

    now2 = 0



    dp = [0]*p

    cnt = 0

    if p != 5:

        for i in s:

            now2 = (now2*10+i) % p

            dp[((i-now2) % p)*pow(now, p-2, p) % p] += 1

            cnt += dp[((-now2) % p)*pow(now, p-2, p) % p]

            now = (now*10) % p

    else:

        for j, i in enumerate(s):

            if i in [0, 5]:

                cnt += j+1

    print(cnt)





main()

n,p=list(map(int,input().split()));s,n,N,c,d=list(map(int,list(eval(input())))),1,0,0,[0]*p

if p!=5:

 for i in s:N,a=(N*10+i)%p,pow(n,p-2,p);d[(i-N)*a%p]+=1;c+=d[-N*a%p];n=n*10%p

else:c=sum([j+1 for j,i in enumerate(s)if i%5==0])

print(c)
def main():
    n, p = list(map(int, input().split()))
    s = list(map(int, list(eval(input()))))
    now = 1
    now2 = 0
    dp = [0] * p
    cnt = 0
    if p != 5:
        for i in s:
            now2 = (now2 * 10 + i) % p
            dp[((i - now2) % p) * pow(now, p - 2, p) % p] += 1
            cnt += dp[((-now2) % p) * pow(now, p - 2, p) % p]
            now = (now * 10) % p
    else:
        for j, i in enumerate(s):
            if i in [0, 5]:
                cnt += j + 1
    print(cnt)


main()

n, p = list(map(int, input().split()))
s, n, N, c, d = list(map(int, list(eval(input())))), 1, 0, 0, [0] * p
if p != 5:
    for i in s:
        N, a = (N * 10 + i) % p, pow(n, p - 2, p)
        d[(i - N) * a % p] += 1
        c += d[-N * a % p]
        n = n * 10 % p
else:
    c = sum([j + 1 for j, i in enumerate(s) if i % 5 == 0])
print(c)

-def main(): -    n, p = list(map(int, input().split())) -    s = list(map(int, list(eval(input())))) -    now = 1 -    now2 = 0 -    dp = [0] * p -    cnt = 0 -    if p != 5: -        for i in s: -            now2 = (now2 * 10 + i) % p -            dp[((i - now2) % p) * pow(now, p - 2, p) % p] += 1 -            cnt += dp[((-now2) % p) * pow(now, p - 2, p) % p] -            now = (now * 10) % p -    else: -        for j, i in enumerate(s): -            if i in [0, 5]: -                cnt += j + 1 -    print(cnt) - - -main() +n, p = list(map(int, input().split())) +s, n, N, c, d = list(map(int, list(eval(input())))), 1, 0, 0, [0] * p +if p != 5: +    for i in s: +        N, a = (N * 10 + i) % p, pow(n, p - 2, p) +        d[(i - N) * a % p] += 1 +        c += d[-N * a % p] +        n = n * 10 % p +else: +    c = sum([j + 1 for j, i in enumerate(s) if i % 5 == 0]) +print(c)