def calc(S, P):

    ans = 0

    if P == 2 or P == 5:

        for i, s in enumerate(S):

            if int(s) % P == 0:

                ans += i + 1

        return ans

    else:

        C = [0] * P

        C[0] = 1

        a = 0

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

            a = (a + int(s) * pow(10, i, P)) % P

            ans += C[a]

            C[a] += 1

        return ans



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

S = eval(input())

print((calc(S, P)))
N, P = list(map(int, input().split()))

S = eval(input())

ans = 0

C = [0] * P

C[0] = 1

a = 0

if P in (2, 5):

    for i, s in enumerate(S):

        if int(s) % P == 0:

            ans += i + 1

    print(ans)

else:

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

        a = (a + int(s) * pow(10, i, P)) % P

        ans += C[a]

        C[a] += 1

    print(ans)
def calc(S, P):
    ans = 0
    if P == 2 or P == 5:
        for i, s in enumerate(S):
            if int(s) % P == 0:
                ans += i + 1
        return ans
    else:
        C = [0] * P
        C[0] = 1
        a = 0
        for i, s in enumerate(S[::-1]):
            a = (a + int(s) * pow(10, i, P)) % P
            ans += C[a]
            C[a] += 1
        return ans


N, P = list(map(int, input().split()))
S = eval(input())
print((calc(S, P)))

N, P = list(map(int, input().split()))
S = eval(input())
ans = 0
C = [0] * P
C[0] = 1
a = 0
if P in (2, 5):
    for i, s in enumerate(S):
        if int(s) % P == 0:
            ans += i + 1
    print(ans)
else:
    for i, s in enumerate(S[::-1]):
        a = (a + int(s) * pow(10, i, P)) % P
        ans += C[a]
        C[a] += 1
    print(ans)

-def calc(S, P): -    ans = 0 -    if P == 2 or P == 5: -        for i, s in enumerate(S): -            if int(s) % P == 0: -                ans += i + 1 -        return ans -    else: -        C = [0] * P -        C[0] = 1 -        a = 0 -        for i, s in enumerate(S[::-1]): -            a = (a + int(s) * pow(10, i, P)) % P -            ans += C[a] -            C[a] += 1 -        return ans - - -print((calc(S, P))) +ans = 0 +C = [0] * P +C[0] = 1 +a = 0 +if P in (2, 5): +    for i, s in enumerate(S): +        if int(s) % P == 0: +            ans += i + 1 +    print(ans) +else: +    for i, s in enumerate(S[::-1]): +        a = (a + int(s) * pow(10, i, P)) % P +        ans += C[a] +        C[a] += 1 +    print(ans)