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()

# 参考にさせていただいたもの

# https://atcoder.jp/contests/abc158/submissions/10620169

# mulをpow(10,桁数,P)ではなく、ループ中に10倍ずつする方法

# counterに収められたある剰余をとる集合の大きさからnC2でペア数を求める方法



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

            counter[curr_num] += 1

            mul = (mul * 10) % P



        def choose_2(x):

            return x * (x - 1) // 2



        ret = sum(choose_2(cnt) for cnt in counter)

        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()

# 参考にさせていただいたもの
# https://atcoder.jp/contests/abc158/submissions/10620169
# mulをpow(10,桁数,P)ではなく、ループ中に10倍ずつする方法
# counterに収められたある剰余をとる集合の大きさからnC2でペア数を求める方法
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
            counter[curr_num] += 1
            mul = (mul * 10) % P

        def choose_2(x):
            return x * (x - 1) // 2

        ret = sum(choose_2(cnt) for cnt in counter)
        print(ret)
        return


if __name__ == "__main__":
    main()

+# 参考にさせていただいたもの +# https://atcoder.jp/contests/abc158/submissions/10620169 +# mulをpow(10,桁数,P)ではなく、ループ中に10倍ずつする方法 +# counterに収められたある剰余をとる集合の大きさからnC2でペア数を求める方法 -            ret += counter[curr_num] + +        def choose_2(x): +            return x * (x - 1) // 2 + +        ret = sum(choose_2(cnt) for cnt in counter)