import sys



def solve():

    input = sys.stdin.readline

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

    S = list(input().strip("\n"))



    if P == 2 or P == 5:

        count = 0

        for i in range(N):

            s = int(S[N - 1 - i])

            if s % P == 0: count += N - i

        print(count)

    else:

        modDict = dict()

        modDict[0] = 1

        sSum = 0

        for i in range(N):

            s = int(S[N - 1 - i])

            sSum += s * pow(10, i, P)

            sSum %= P

            if sSum in modDict: modDict[sSum] += 1

            else: modDict[sSum] = 1

        count = 0

        for key in modDict:

            count += modDict[key] * (modDict[key] - 1) // 2

        print(count)

            



    return 0



if __name__ == "__main__":

    solve()
import sys

from collections import defaultdict



def solve():

    input = sys.stdin.readline

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

    S = input().strip("\n")

    modSum = 0

    modDict = defaultdict(int)

    modDict[0] += 1

    ans = 0



    if P == 2 or P == 5:

        for i in range(N):

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

                ans += i + 1

        

    else:

        for i in range(N):

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

            modSum %= P

            modDict[modSum] += 1



        for key in modDict:

            v = modDict[key]

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

    

    print(ans)



    return 0



if __name__ == "__main__":

    solve()
import sys


def solve():
    input = sys.stdin.readline
    N, P = list(map(int, input().split()))
    S = list(input().strip("\n"))
    if P == 2 or P == 5:
        count = 0
        for i in range(N):
            s = int(S[N - 1 - i])
            if s % P == 0:
                count += N - i
        print(count)
    else:
        modDict = dict()
        modDict[0] = 1
        sSum = 0
        for i in range(N):
            s = int(S[N - 1 - i])
            sSum += s * pow(10, i, P)
            sSum %= P
            if sSum in modDict:
                modDict[sSum] += 1
            else:
                modDict[sSum] = 1
        count = 0
        for key in modDict:
            count += modDict[key] * (modDict[key] - 1) // 2
        print(count)
    return 0


if __name__ == "__main__":
    solve()

import sys
from collections import defaultdict


def solve():
    input = sys.stdin.readline
    N, P = list(map(int, input().split()))
    S = input().strip("\n")
    modSum = 0
    modDict = defaultdict(int)
    modDict[0] += 1
    ans = 0
    if P == 2 or P == 5:
        for i in range(N):
            if int(S[i]) % P == 0:
                ans += i + 1
    else:
        for i in range(N):
            modSum += int(S[N - i - 1]) * pow(10, i, P)
            modSum %= P
            modDict[modSum] += 1
        for key in modDict:
            v = modDict[key]
            ans += v * (v - 1) // 2
    print(ans)
    return 0


if __name__ == "__main__":
    solve()

+from collections import defaultdict -    S = list(input().strip("\n")) +    S = input().strip("\n") +    modSum = 0 +    modDict = defaultdict(int) +    modDict[0] += 1 +    ans = 0 -        count = 0 -            s = int(S[N - 1 - i]) -            if s % P == 0: -                count += N - i -        print(count) +            if int(S[i]) % P == 0: +                ans += i + 1 -        modDict = dict() -        modDict[0] = 1 -        sSum = 0 -            s = int(S[N - 1 - i]) -            sSum += s * pow(10, i, P) -            sSum %= P -            if sSum in modDict: -                modDict[sSum] += 1 -            else: -                modDict[sSum] = 1 -        count = 0 +            modSum += int(S[N - i - 1]) * pow(10, i, P) +            modSum %= P +            modDict[modSum] += 1 -            count += modDict[key] * (modDict[key] - 1) // 2 -        print(count) +            v = modDict[key] +            ans += v * (v - 1) // 2 +    print(ans)