n, p = list(map(int, input().split()))



s = eval(input())



if 10 % p == 0:

    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)

    print(ans)

else:

    counter = [0] * p

    counter[0] = 1

    cur = 0

    p10 = 1

    ans = 0

    for x in s[::-1]:

        cur = (cur + int(x) * p10) % p

        ans += counter[cur]

        counter[cur] += 1

        p10 *= 10

        p10 %= p

    print(ans)

def main():

    import sys

    input = sys.stdin.readline

    from collections import defaultdict

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

    S = input().rstrip()

    if P in (2, 5):

        ans = 0

        for i, c in enumerate(S):

            if int(c) % P == 0:

                ans += i + 1

        print(ans)

        sys.exit()

    num = 0

    d = 1

    counter = defaultdict(int)

    counter[0] += 1

    for c in S[::-1]:

        num += int(c) * d

        num %= P

        counter[num] += 1

        d *= 10

        d %= P

    ans = 0

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

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

    print(ans)



if __name__ == '__main__':

    main()
n, p = list(map(int, input().split()))
s = eval(input())
if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)

def main():
    import sys

    input = sys.stdin.readline
    from collections import defaultdict

    N, P = list(map(int, input().split()))
    S = input().rstrip()
    if P in (2, 5):
        ans = 0
        for i, c in enumerate(S):
            if int(c) % P == 0:
                ans += i + 1
        print(ans)
        sys.exit()
    num = 0
    d = 1
    counter = defaultdict(int)
    counter[0] += 1
    for c in S[::-1]:
        num += int(c) * d
        num %= P
        counter[num] += 1
        d *= 10
        d %= P
    ans = 0
    for v in list(counter.values()):
        ans += (v * (v - 1)) // 2
    print(ans)


if __name__ == "__main__":
    main()

-n, p = list(map(int, input().split())) -s = eval(input()) -if 10 % p == 0: -    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0) +def main(): +    import sys + +    input = sys.stdin.readline +    from collections import defaultdict + +    N, P = list(map(int, input().split())) +    S = input().rstrip() +    if P in (2, 5): +        ans = 0 +        for i, c in enumerate(S): +            if int(c) % P == 0: +                ans += i + 1 +        print(ans) +        sys.exit() +    num = 0 +    d = 1 +    counter = defaultdict(int) +    counter[0] += 1 +    for c in S[::-1]: +        num += int(c) * d +        num %= P +        counter[num] += 1 +        d *= 10 +        d %= P +    ans = 0 +    for v in list(counter.values()): +        ans += (v * (v - 1)) // 2 -else: -    counter = [0] * p -    counter[0] = 1 -    cur = 0 -    p10 = 1 -    ans = 0 -    for x in s[::-1]: -        cur = (cur + int(x) * p10) % p -        ans += counter[cur] -        counter[cur] += 1 -        p10 *= 10 -        p10 %= p -    print(ans) + + +if __name__ == "__main__": +    main()