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

S = eval(input())



if p == 2 or p == 5:

  ans = 0

  for i,s in enumerate(S):

    if int(s)%p == 0:

      ans += i+1

  print(ans)

  exit()



def MS(i,s,pre):

  return (int(s)*i+pre)%p

 

M = [0]*p

M[0] = 1

i = 1

pre = 0

for s in S[::-1]:

  #pre = MS(i,int(s),pre)

  pre += int(s)*i

  pre %= p

  M[pre] += 1

  i = i*10%p



ans = sum([(m*(m-1))//2 for m in M])

print(ans)



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

S = eval(input())



if p == 2 or p == 5:

  ans = 0

  for i,s in enumerate(S):

    if int(s)%p == 0:

      ans += i+1

  print(ans)

  exit()



def MS(i,s,pre):

  return (int(s)*i+pre)%p

 

M = [0]*p

M[0] = 1

i = 1

pre = 0

for s in S[::-1]:

  #pre = MS(i,int(s),pre)

  pre = pre + int(s)*i

  pre = pre%p

  M[pre] += 1

  i = i*10%p



ans = sum([(m*(m-1))//2 for m in M])

print(ans)



n, p = list(map(int, input().split()))
S = eval(input())
if p == 2 or p == 5:
    ans = 0
    for i, s in enumerate(S):
        if int(s) % p == 0:
            ans += i + 1
    print(ans)
    exit()


def MS(i, s, pre):
    return (int(s) * i + pre) % p


M = [0] * p
M[0] = 1
i = 1
pre = 0
for s in S[::-1]:
    # pre = MS(i,int(s),pre)
    pre += int(s) * i
    pre %= p
    M[pre] += 1
    i = i * 10 % p
ans = sum([(m * (m - 1)) // 2 for m in M])
print(ans)

n, p = list(map(int, input().split()))
S = eval(input())
if p == 2 or p == 5:
    ans = 0
    for i, s in enumerate(S):
        if int(s) % p == 0:
            ans += i + 1
    print(ans)
    exit()


def MS(i, s, pre):
    return (int(s) * i + pre) % p


M = [0] * p
M[0] = 1
i = 1
pre = 0
for s in S[::-1]:
    # pre = MS(i,int(s),pre)
    pre = pre + int(s) * i
    pre = pre % p
    M[pre] += 1
    i = i * 10 % p
ans = sum([(m * (m - 1)) // 2 for m in M])
print(ans)

-    pre += int(s) * i -    pre %= p +    pre = pre + int(s) * i +    pre = pre % p