

def I(): return int(eval(input()))

def MI(): return list(map(int, input().split()))

def LI(): return list(map(int, input().split()))



def main():

    N,P=MI()

    s=eval(input())

    ans=0

    if P==2 or P==5:

        for i in range(N):

            t=int(s[i])

            if t%P==0:

                ans+=i+1

            

    else:

        ans=0

        t=0

        mod=[0]*P

        mod[0]=1

        for i in range(N):

            t=t+int(s[N-i-1])*pow(10,i,P)

            t=t%P

            mod[t]+=1

            if mod[t]>=2:

                ans+=mod[t]-1

                



            



    print(ans)   

        

    

            

    

        

    



main()



def I(): return int(eval(input()))

def MI(): return list(map(int, input().split()))

def LI(): return list(map(int, input().split()))



def main():

    N,P=MI()

    S=eval(input())

    

    if P==2 or P==5:

        ans=0

        for i in range(N):

            if int(S[i])%P==0:

                ans+=i+1

                

        print(ans)

        exit()

    

    

    

    from collections import defaultdict

    dd = defaultdict(int)

    

    now=0

    dd[0]=1

    for i in range(N):

        now=now+(pow(10,i,P)*int(S[-1-i]))

        now%=P

        dd[now]+=1

        

    ans=0

    for k,v in list(dd.items()):

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

        

    print(ans)

    



main()

def I():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


def LI():
    return list(map(int, input().split()))


def main():
    N, P = MI()
    s = eval(input())
    ans = 0
    if P == 2 or P == 5:
        for i in range(N):
            t = int(s[i])
            if t % P == 0:
                ans += i + 1
    else:
        ans = 0
        t = 0
        mod = [0] * P
        mod[0] = 1
        for i in range(N):
            t = t + int(s[N - i - 1]) * pow(10, i, P)
            t = t % P
            mod[t] += 1
            if mod[t] >= 2:
                ans += mod[t] - 1
    print(ans)


main()

def I():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


def LI():
    return list(map(int, input().split()))


def main():
    N, P = MI()
    S = eval(input())
    if P == 2 or P == 5:
        ans = 0
        for i in range(N):
            if int(S[i]) % P == 0:
                ans += i + 1
        print(ans)
        exit()
    from collections import defaultdict

    dd = defaultdict(int)
    now = 0
    dd[0] = 1
    for i in range(N):
        now = now + (pow(10, i, P) * int(S[-1 - i]))
        now %= P
        dd[now] += 1
    ans = 0
    for k, v in list(dd.items()):
        ans += (v * (v - 1)) // 2
    print(ans)


main()

-    s = eval(input()) +    S = eval(input()) +    if P == 2 or P == 5: +        ans = 0 +        for i in range(N): +            if int(S[i]) % P == 0: +                ans += i + 1 +        print(ans) +        exit() +    from collections import defaultdict + +    dd = defaultdict(int) +    now = 0 +    dd[0] = 1 +    for i in range(N): +        now = now + (pow(10, i, P) * int(S[-1 - i])) +        now %= P +        dd[now] += 1 -    if P == 2 or P == 5: -        for i in range(N): -            t = int(s[i]) -            if t % P == 0: -                ans += i + 1 -    else: -        ans = 0 -        t = 0 -        mod = [0] * P -        mod[0] = 1 -        for i in range(N): -            t = t + int(s[N - i - 1]) * pow(10, i, P) -            t = t % P -            mod[t] += 1 -            if mod[t] >= 2: -                ans += mod[t] - 1 +    for k, v in list(dd.items()): +        ans += (v * (v - 1)) // 2