from collections import Counter



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

s = eval(input())



if p == 2 or p == 5:

    ans = 0

    for i, e in enumerate(s, 1):

        if int(e) % p == 0:

            ans += i



    print(ans)



else:

    li = [0]

    for i, e in enumerate(s[::-1], 1):

        li.append((li[-1] + int(e) * pow(10, i, p)) % p)



    c = Counter(li)

    ans = 0

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

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



    print(ans)

from collections import Counter



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

s = eval(input())



if p == 2 or p == 5:

    d = {2: "02468", 5: "05"}

    ans = 0

    for i, e in enumerate(s, 1):

        if e in d[p]:

            ans += i



    print(ans)



else:

    li = [0]

    power = 1

    for e in s[::-1]:

        power *= 10

        power %= p

        li.append((li[-1] + int(e) * power) % p)



    c = Counter(li)

    ans = 0

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

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



    print(ans)

from collections import Counter

n, p = list(map(int, input().split()))
s = eval(input())
if p == 2 or p == 5:
    ans = 0
    for i, e in enumerate(s, 1):
        if int(e) % p == 0:
            ans += i
    print(ans)
else:
    li = [0]
    for i, e in enumerate(s[::-1], 1):
        li.append((li[-1] + int(e) * pow(10, i, p)) % p)
    c = Counter(li)
    ans = 0
    for v in list(c.values()):
        ans += v * (v - 1) // 2
    print(ans)

from collections import Counter

n, p = list(map(int, input().split()))
s = eval(input())
if p == 2 or p == 5:
    d = {2: "02468", 5: "05"}
    ans = 0
    for i, e in enumerate(s, 1):
        if e in d[p]:
            ans += i
    print(ans)
else:
    li = [0]
    power = 1
    for e in s[::-1]:
        power *= 10
        power %= p
        li.append((li[-1] + int(e) * power) % p)
    c = Counter(li)
    ans = 0
    for v in list(c.values()):
        ans += v * (v - 1) // 2
    print(ans)

+    d = {2: "02468", 5: "05"} -        if int(e) % p == 0: +        if e in d[p]: -    for i, e in enumerate(s[::-1], 1): -        li.append((li[-1] + int(e) * pow(10, i, p)) % p) +    power = 1 +    for e in s[::-1]: +        power *= 10 +        power %= p +        li.append((li[-1] + int(e) * power) % p)