def main():

    N, P = (int(i) for i in input().split())

    L = [int(s) for s in eval(input())][::-1]

    ans = 0

    if P == 2 or P == 5:

        for i, e in enumerate(L):

            if e % P == 0:

                ans += N-i

    else:

        A = [0]*N

        d = 1

        for i, e in enumerate(L):

            A[i] = (e*d) % P

            d *= 10

            d %= P

        S = [0]*(N+1)

        for i in range(N):

            S[i+1] = S[i] + A[i]

            S[i+1] %= P

        from collections import Counter

        c = Counter(S)

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

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

    print(ans)





if __name__ == '__main__':

    main()

def main():

    _, P = (int(i) for i in input().split())

    S = [int(s) for s in eval(input())]

    ans = 0

    if P == 2:

        for i, v in enumerate(S, start=1):

            if v % 2 == 0:

                ans += i

    elif P == 5:

        for i, v in enumerate(S, start=1):

            if v == 0 or v == 5:

                ans += i

    else:

        cnt = [0]*P

        d = 1

        pre = 0

        cnt[pre] += 1

        for v in S[::-1]:

            v *= d

            v += pre

            v %= P

            cnt[v] += 1

            d *= 10

            d %= P

            pre = v



        ans = sum(cnt[i]*(cnt[i]-1)//2 for i in range(P))



    print(ans)





if __name__ == '__main__':

    main()

def main():
    N, P = (int(i) for i in input().split())
    L = [int(s) for s in eval(input())][::-1]
    ans = 0
    if P == 2 or P == 5:
        for i, e in enumerate(L):
            if e % P == 0:
                ans += N - i
    else:
        A = [0] * N
        d = 1
        for i, e in enumerate(L):
            A[i] = (e * d) % P
            d *= 10
            d %= P
        S = [0] * (N + 1)
        for i in range(N):
            S[i + 1] = S[i] + A[i]
            S[i + 1] %= P
        from collections import Counter

        c = Counter(S)
        for v in list(c.values()):
            ans += v * (v - 1) // 2
    print(ans)


if __name__ == "__main__":
    main()

def main():
    _, P = (int(i) for i in input().split())
    S = [int(s) for s in eval(input())]
    ans = 0
    if P == 2:
        for i, v in enumerate(S, start=1):
            if v % 2 == 0:
                ans += i
    elif P == 5:
        for i, v in enumerate(S, start=1):
            if v == 0 or v == 5:
                ans += i
    else:
        cnt = [0] * P
        d = 1
        pre = 0
        cnt[pre] += 1
        for v in S[::-1]:
            v *= d
            v += pre
            v %= P
            cnt[v] += 1
            d *= 10
            d %= P
            pre = v
        ans = sum(cnt[i] * (cnt[i] - 1) // 2 for i in range(P))
    print(ans)


if __name__ == "__main__":
    main()

-    N, P = (int(i) for i in input().split()) -    L = [int(s) for s in eval(input())][::-1] +    _, P = (int(i) for i in input().split()) +    S = [int(s) for s in eval(input())] -    if P == 2 or P == 5: -        for i, e in enumerate(L): -            if e % P == 0: -                ans += N - i +    if P == 2: +        for i, v in enumerate(S, start=1): +            if v % 2 == 0: +                ans += i +    elif P == 5: +        for i, v in enumerate(S, start=1): +            if v == 0 or v == 5: +                ans += i -        A = [0] * N +        cnt = [0] * P -        for i, e in enumerate(L): -            A[i] = (e * d) % P +        pre = 0 +        cnt[pre] += 1 +        for v in S[::-1]: +            v *= d +            v += pre +            v %= P +            cnt[v] += 1 -        S = [0] * (N + 1) -        for i in range(N): -            S[i + 1] = S[i] + A[i] -            S[i + 1] %= P -        from collections import Counter - -        c = Counter(S) -        for v in list(c.values()): -            ans += v * (v - 1) // 2 +            pre = v +        ans = sum(cnt[i] * (cnt[i] - 1) // 2 for i in range(P))