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

s = eval(input())

modlist = [0]*p

modlist[0] = 1

if p!= 2 and p != 5:

    mod = 0

    pwrmod = 1

    for i in s[::-1]:

        mod = (mod + pwrmod * int(i)) % p

        pwrmod = (pwrmod * 10) % p

        modlist[mod] += 1

    ans = 0

    for i in modlist:

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



    print(ans)

else:

    ans = 0

    for i in range(n):

        if int(s[i]) % p == 0:

            ans += i+1

    print(ans)

n,p = tuple(int(x) for x in input().split())

s = [int(s) for s in eval(input())][::-1]

modlist = [0]*p

modlist[0] = 1

if p!= 2 and p != 5:

    mod = 0

    pwrmod = 1

    for i in s:

        mod = (mod + pwrmod * i) % p

        pwrmod = (pwrmod * 10) % p

        modlist[mod] += 1

    ans = 0

    for i in modlist:

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



    print(ans)

else:

    ans = 0

    for i in range(n):

        if s[i] % p == 0:

            ans += n-i

    print(ans)

n, p = tuple(map(int, input().split()))
s = eval(input())
modlist = [0] * p
modlist[0] = 1
if p != 2 and p != 5:
    mod = 0
    pwrmod = 1
    for i in s[::-1]:
        mod = (mod + pwrmod * int(i)) % p
        pwrmod = (pwrmod * 10) % p
        modlist[mod] += 1
    ans = 0
    for i in modlist:
        ans += (i * (i - 1)) // 2
    print(ans)
else:
    ans = 0
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i + 1
    print(ans)

n, p = tuple(int(x) for x in input().split())
s = [int(s) for s in eval(input())][::-1]
modlist = [0] * p
modlist[0] = 1
if p != 2 and p != 5:
    mod = 0
    pwrmod = 1
    for i in s:
        mod = (mod + pwrmod * i) % p
        pwrmod = (pwrmod * 10) % p
        modlist[mod] += 1
    ans = 0
    for i in modlist:
        ans += (i * (i - 1)) // 2
    print(ans)
else:
    ans = 0
    for i in range(n):
        if s[i] % p == 0:
            ans += n - i
    print(ans)

-n, p = tuple(map(int, input().split())) -s = eval(input()) +n, p = tuple(int(x) for x in input().split()) +s = [int(s) for s in eval(input())][::-1] -    for i in s[::-1]: -        mod = (mod + pwrmod * int(i)) % p +    for i in s: +        mod = (mod + pwrmod * i) % p -        if int(s[i]) % p == 0: -            ans += i + 1 +        if s[i] % p == 0: +            ans += n - i