def main():

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

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



    ret = 0

    if P in {2, 5}:

        for i, x in enumerate(S, 1):

            if x % P == 0:

                ret += i

        print(ret)

        return



    else:

        curr_num = 0

        counter = [0] * P

        counter[0] = 1

        for i, x in enumerate(reversed(S)):

            curr_num = (curr_num + x * pow(10, i, P)) % P

            ret += counter[curr_num]

            counter[curr_num] += 1

        print(ret)

        return





if __name__ == '__main__':

    main()

def main():

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

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



    ret = 0

    if P in {2, 5}:

        for i, x in enumerate(S, 1):

            if x % P == 0:

                ret += i

        print(ret)

        return



    else:

        curr_num = 0

        counter = [0] * P

        counter[0] = 1

        mul = 1

        for i, x in enumerate(reversed(S)):

            curr_num = (curr_num + x * mul) % P

            ret += counter[curr_num]

            counter[curr_num] += 1

            mul = (mul * 10) % P

        print(ret)

        return





if __name__ == '__main__':

    main()

def main():
    N, P = list(map(int, input().split()))
    S = [int(c) for c in eval(input())]
    ret = 0
    if P in {2, 5}:
        for i, x in enumerate(S, 1):
            if x % P == 0:
                ret += i
        print(ret)
        return
    else:
        curr_num = 0
        counter = [0] * P
        counter[0] = 1
        for i, x in enumerate(reversed(S)):
            curr_num = (curr_num + x * pow(10, i, P)) % P
            ret += counter[curr_num]
            counter[curr_num] += 1
        print(ret)
        return


if __name__ == "__main__":
    main()

def main():
    N, P = list(map(int, input().split()))
    S = [int(c) for c in eval(input())]
    ret = 0
    if P in {2, 5}:
        for i, x in enumerate(S, 1):
            if x % P == 0:
                ret += i
        print(ret)
        return
    else:
        curr_num = 0
        counter = [0] * P
        counter[0] = 1
        mul = 1
        for i, x in enumerate(reversed(S)):
            curr_num = (curr_num + x * mul) % P
            ret += counter[curr_num]
            counter[curr_num] += 1
            mul = (mul * 10) % P
        print(ret)
        return


if __name__ == "__main__":
    main()

+        mul = 1 -            curr_num = (curr_num + x * pow(10, i, P)) % P +            curr_num = (curr_num + x * mul) % P +            mul = (mul * 10) % P