from collections import defaultdict



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

S = eval(input())



def solMin():

    dp = [[0] * P for _ in range(N + 1)]



    for i, s in enumerate(map(int, S), start=1):

        dp[i][s % P] += 1

        for p in range(P):

            dp[i][(p * 10 + s) % P] += dp[i - 1][p]



    return sum(dp[i][0] for i in range(N + 1))



def solLarge():

    cnt = defaultdict(int)

    ret = 0

    now = 0



    for d, s in enumerate(map(int, S[:: -1])):

        now += pow(10, d, P) * s

        now %= P

        ret += cnt[(now - P) % P]

        if now == 0:

            ret += 1

        cnt[now] += 1



    return ret



print((solMin() if P * N <= 10**6 else solLarge()))

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

S = eval(input())



def smallSol():

    dp = [0] * P

    ans = 0

    for s in map(int, S):

        newDp = [0] * P

        newDp[s % P] += 1

        for p in range(P):

            newDp[(p * 10 + s) % P] += dp[p]

        dp = newDp

        ans += dp[0]

    return ans



def largeSol():

    cnt = [0] * P

    cnt[0] += 1

    ret = 0

    now = 0

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

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

        ret += cnt[now - P]

        cnt[now] += 1

    return ret



print((smallSol() if N * P <= 10**6 else largeSol()))

from collections import defaultdict

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


def solMin():
    dp = [[0] * P for _ in range(N + 1)]
    for i, s in enumerate(map(int, S), start=1):
        dp[i][s % P] += 1
        for p in range(P):
            dp[i][(p * 10 + s) % P] += dp[i - 1][p]
    return sum(dp[i][0] for i in range(N + 1))


def solLarge():
    cnt = defaultdict(int)
    ret = 0
    now = 0
    for d, s in enumerate(map(int, S[::-1])):
        now += pow(10, d, P) * s
        now %= P
        ret += cnt[(now - P) % P]
        if now == 0:
            ret += 1
        cnt[now] += 1
    return ret


print((solMin() if P * N <= 10**6 else solLarge()))

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


def smallSol():
    dp = [0] * P
    ans = 0
    for s in map(int, S):
        newDp = [0] * P
        newDp[s % P] += 1
        for p in range(P):
            newDp[(p * 10 + s) % P] += dp[p]
        dp = newDp
        ans += dp[0]
    return ans


def largeSol():
    cnt = [0] * P
    cnt[0] += 1
    ret = 0
    now = 0
    for i, s in enumerate(map(int, S[::-1])):
        now = (pow(10, i, P) * s + now) % P
        ret += cnt[now - P]
        cnt[now] += 1
    return ret


print((smallSol() if N * P <= 10**6 else largeSol()))

-from collections import defaultdict - -def solMin(): -    dp = [[0] * P for _ in range(N + 1)] -    for i, s in enumerate(map(int, S), start=1): -        dp[i][s % P] += 1 +def smallSol(): +    dp = [0] * P +    ans = 0 +    for s in map(int, S): +        newDp = [0] * P +        newDp[s % P] += 1 -            dp[i][(p * 10 + s) % P] += dp[i - 1][p] -    return sum(dp[i][0] for i in range(N + 1)) +            newDp[(p * 10 + s) % P] += dp[p] +        dp = newDp +        ans += dp[0] +    return ans -def solLarge(): -    cnt = defaultdict(int) +def largeSol(): +    cnt = [0] * P +    cnt[0] += 1 -    for d, s in enumerate(map(int, S[::-1])): -        now += pow(10, d, P) * s -        now %= P -        ret += cnt[(now - P) % P] -        if now == 0: -            ret += 1 +    for i, s in enumerate(map(int, S[::-1])): +        now = (pow(10, i, P) * s + now) % P +        ret += cnt[now - P] -print((solMin() if P * N <= 10**6 else solLarge())) +print((smallSol() if N * P <= 10**6 else largeSol()))