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

S = eval(input())



res = 0



if P == 2 or P == 5:

    # Corner case

    for i in range(N):

        if int(S[N - i - 1]) % P == 0:

            res += N - i

else:

    val = [0] * P

    tenfactor = 1

    cur = 0

    val[cur] += 1

    for i in range(N):

        cur = (cur + int(S[N - i - 1]) * tenfactor) % P

        tenfactor = (tenfactor * 10) % P

        val[cur] += 1

        

    # Combination

    for p in range(P):

        res += val[p] * (val[p] - 1) // 2

    

print(res)





from collections import defaultdict



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

S = eval(input())



if P == 2 or P == 5:

    ans = 0

    for i in range(N):

        if int(S[i]) % P == 0:

            ans += i + 1

    print(ans)

else:

    ctr = defaultdict(int)

    ctr[0] = 1

    cur = 0

    for i in reversed(list(range(N))):

        cur = (cur + int(S[i]) * pow(10, N - i - 1, P)) % P

        ctr[cur] += 1



    ans = 0

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

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

    print(ans)

N, P = list(map(int, input().split()))
S = eval(input())
res = 0
if P == 2 or P == 5:
    # Corner case
    for i in range(N):
        if int(S[N - i - 1]) % P == 0:
            res += N - i
else:
    val = [0] * P
    tenfactor = 1
    cur = 0
    val[cur] += 1
    for i in range(N):
        cur = (cur + int(S[N - i - 1]) * tenfactor) % P
        tenfactor = (tenfactor * 10) % P
        val[cur] += 1
    # Combination
    for p in range(P):
        res += val[p] * (val[p] - 1) // 2
print(res)

from collections import defaultdict

N, P = list(map(int, input().split()))
S = eval(input())
if P == 2 or P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) % P == 0:
            ans += i + 1
    print(ans)
else:
    ctr = defaultdict(int)
    ctr[0] = 1
    cur = 0
    for i in reversed(list(range(N))):
        cur = (cur + int(S[i]) * pow(10, N - i - 1, P)) % P
        ctr[cur] += 1
    ans = 0
    for v in list(ctr.values()):
        ans += v * (v - 1) // 2
    print(ans)

+from collections import defaultdict + -res = 0 -    # Corner case +    ans = 0 -        if int(S[N - i - 1]) % P == 0: -            res += N - i +        if int(S[i]) % P == 0: +            ans += i + 1 +    print(ans) -    val = [0] * P -    tenfactor = 1 +    ctr = defaultdict(int) +    ctr[0] = 1 -    val[cur] += 1 -    for i in range(N): -        cur = (cur + int(S[N - i - 1]) * tenfactor) % P -        tenfactor = (tenfactor * 10) % P -        val[cur] += 1 -    # Combination -    for p in range(P): -        res += val[p] * (val[p] - 1) // 2 -print(res) +    for i in reversed(list(range(N))): +        cur = (cur + int(S[i]) * pow(10, N - i - 1, P)) % P +        ctr[cur] += 1 +    ans = 0 +    for v in list(ctr.values()): +        ans += v * (v - 1) // 2 +    print(ans)