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

s = list(map(int, list(eval(input()))))[::-1]



sum = 0

cnt = [0] * p

cnt[0] = 1

n = len(s)

for i in range(n):

    d = s[i] * pow(10, i, p)

    sum = (sum + d) % p

    cnt[sum] += 1



ans = 0

if p in [2, 5]:

    if p == 2:

        ok_list = [0, 2, 4, 6, 8]

    if p == 5:

        ok_list = [0, 5]



    for i in range(n):

        d = s[i]

        if d in ok_list:

            ans += n - i

           

else:

    for i in range(p):

        ans += cnt[i] * (cnt[i]-1) // 2





print(ans)

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

from collections import Counter



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

S = list(map(int, read().rstrip().decode()))



def solve_2(S):

    ind = (i for i, x in enumerate(S, 1) if x % 2 == 0)

    return sum(ind)



def solve_5(S):

    ind = (i for i, x in enumerate(S, 1) if x % 5 == 0)

    return sum(ind)



def solve(S,P):

    if P == 2:

        return solve_2(S)

    if P == 5:

        return solve_5(S)

    S = S[::-1]

    T = [0] * len(S)

    T[0] = S[0] % P

    power = 1

    for i in range(1, len(S)):

        power *= 10

        power %= P

        T[i] = T[i-1] + power * S[i]

        T[i] %= P

    counter = Counter(T)

    return sum(x * (x - 1) // 2 for x in list(counter.values())) + counter[0]





print((solve(S,P)))

n, p = list(map(int, input().split()))
s = list(map(int, list(eval(input()))))[::-1]
sum = 0
cnt = [0] * p
cnt[0] = 1
n = len(s)
for i in range(n):
    d = s[i] * pow(10, i, p)
    sum = (sum + d) % p
    cnt[sum] += 1
ans = 0
if p in [2, 5]:
    if p == 2:
        ok_list = [0, 2, 4, 6, 8]
    if p == 5:
        ok_list = [0, 5]
    for i in range(n):
        d = s[i]
        if d in ok_list:
            ans += n - i
else:
    for i in range(p):
        ans += cnt[i] * (cnt[i] - 1) // 2
print(ans)

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import Counter

N, P = list(map(int, readline().split()))
S = list(map(int, read().rstrip().decode()))


def solve_2(S):
    ind = (i for i, x in enumerate(S, 1) if x % 2 == 0)
    return sum(ind)


def solve_5(S):
    ind = (i for i, x in enumerate(S, 1) if x % 5 == 0)
    return sum(ind)


def solve(S, P):
    if P == 2:
        return solve_2(S)
    if P == 5:
        return solve_5(S)
    S = S[::-1]
    T = [0] * len(S)
    T[0] = S[0] % P
    power = 1
    for i in range(1, len(S)):
        power *= 10
        power %= P
        T[i] = T[i - 1] + power * S[i]
        T[i] %= P
    counter = Counter(T)
    return sum(x * (x - 1) // 2 for x in list(counter.values())) + counter[0]


print((solve(S, P)))

-n, p = list(map(int, input().split())) -s = list(map(int, list(eval(input()))))[::-1] -sum = 0 -cnt = [0] * p -cnt[0] = 1 -n = len(s) -for i in range(n): -    d = s[i] * pow(10, i, p) -    sum = (sum + d) % p -    cnt[sum] += 1 -ans = 0 -if p in [2, 5]: -    if p == 2: -        ok_list = [0, 2, 4, 6, 8] -    if p == 5: -        ok_list = [0, 5] -    for i in range(n): -        d = s[i] -        if d in ok_list: -            ans += n - i -else: -    for i in range(p): -        ans += cnt[i] * (cnt[i] - 1) // 2 -print(ans) +import sys + +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines +from collections import Counter + +N, P = list(map(int, readline().split())) +S = list(map(int, read().rstrip().decode())) + + +def solve_2(S): +    ind = (i for i, x in enumerate(S, 1) if x % 2 == 0) +    return sum(ind) + + +def solve_5(S): +    ind = (i for i, x in enumerate(S, 1) if x % 5 == 0) +    return sum(ind) + + +def solve(S, P): +    if P == 2: +        return solve_2(S) +    if P == 5: +        return solve_5(S) +    S = S[::-1] +    T = [0] * len(S) +    T[0] = S[0] % P +    power = 1 +    for i in range(1, len(S)): +        power *= 10 +        power %= P +        T[i] = T[i - 1] + power * S[i] +        T[i] %= P +    counter = Counter(T) +    return sum(x * (x - 1) // 2 for x in list(counter.values())) + counter[0] + + +print((solve(S, P)))