n,p = [int(i) for i in input().split()]

s = eval(input())

ans = 0

import collections



if p == 2 or p == 5:

    for i in range(n):

        if int(s[i]) % p == 0:

            ans += i + 1

    print(ans)

    exit()



else:

  chk = []

  m = 0

  chk = collections.deque(chk)

  for i in range(n)[::-1]:

    m = (int(s[i])*pow(10,n-1-i,p)+m)%p

    chk.append(m)

  chk.append(0)

  chk = list(chk)

  c = collections.Counter(chk)

  for i in range(p):

    ans += c[i]*(c[i]-1)/2

  print((int(ans)))
import sys

input = sys.stdin.readline

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

s=eval(input())

ans=0



if p == 2 or p == 5:

  for i in range(n):

    if int(s[i]) % p == 0:

        ans += i + 1

  print(ans)

  exit()

    

total=[0]*p#pで割った余りで分類

total[0]=1#t[0]=0の分をあらかじめカウント

t=[0]*(n+1)#s[i:n]を格納する用の配列

for i in range(n-1,-1,-1):

    t[i]=t[i+1]+int(s[i])*pow(10,n-1-i,p)#s[i:n]を漸化式で付け加えていく

    total[t[i]%p]+=1

    

for i in range(p):

    ans+=total[i]*(total[i]-1)//2

print(ans)
n, p = [int(i) for i in input().split()]
s = eval(input())
ans = 0
import collections

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i + 1
    print(ans)
    exit()
else:
    chk = []
    m = 0
    chk = collections.deque(chk)
    for i in range(n)[::-1]:
        m = (int(s[i]) * pow(10, n - 1 - i, p) + m) % p
        chk.append(m)
    chk.append(0)
    chk = list(chk)
    c = collections.Counter(chk)
    for i in range(p):
        ans += c[i] * (c[i] - 1) / 2
    print((int(ans)))

import sys

input = sys.stdin.readline
n, p = list(map(int, input().split()))
s = eval(input())
ans = 0
if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i + 1
    print(ans)
    exit()
total = [0] * p  # pで割った余りで分類
total[0] = 1  # t[0]=0の分をあらかじめカウント
t = [0] * (n + 1)  # s[i:n]を格納する用の配列
for i in range(n - 1, -1, -1):
    t[i] = t[i + 1] + int(s[i]) * pow(10, n - 1 - i, p)  # s[i:n]を漸化式で付け加えていく
    total[t[i] % p] += 1
for i in range(p):
    ans += total[i] * (total[i] - 1) // 2
print(ans)

-n, p = [int(i) for i in input().split()] +import sys + +input = sys.stdin.readline +n, p = list(map(int, input().split())) -import collections - -else: -    chk = [] -    m = 0 -    chk = collections.deque(chk) -    for i in range(n)[::-1]: -        m = (int(s[i]) * pow(10, n - 1 - i, p) + m) % p -        chk.append(m) -    chk.append(0) -    chk = list(chk) -    c = collections.Counter(chk) -    for i in range(p): -        ans += c[i] * (c[i] - 1) / 2 -    print((int(ans))) +total = [0] * p  # pで割った余りで分類 +total[0] = 1  # t[0]=0の分をあらかじめカウント +t = [0] * (n + 1)  # s[i:n]を格納する用の配列 +for i in range(n - 1, -1, -1): +    t[i] = t[i + 1] + int(s[i]) * pow(10, n - 1 - i, p)  # s[i:n]を漸化式で付け加えていく +    total[t[i] % p] += 1 +for i in range(p): +    ans += total[i] * (total[i] - 1) // 2 +print(ans)