import sys



stdin = sys.stdin



ns = lambda: stdin.readline().rstrip()

ni = lambda: int(stdin.readline().rstrip())

nm = lambda: list(map(int, stdin.readline().split()))

nl = lambda: list(map(int, stdin.readline().split()))



n,p = nm()

s = list(map(int, list(ns())))

ans = 0

if p == 2:

  for i in range(n):

    if s[i]%2 == 0:

      ans += i+1

  print(ans); exit(0)

if p == 5:

  for i in range(n):

    if s[i]%5 == 0:

      ans += i+1

  print(ans); exit(0)

   

g = [0]*(n+1)

d = [0]*p

d[0] = 1

t = 1

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

    g[i] = (g[i+1] + s[i]*t)%p

    ans += d[g[i]]

    d[g[i]] += 1

    t = (t*10)%p

print(ans)

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

s = list(map(int, list(eval(input()))))

if p in (2, 5):

  print((sum(i+1 for i in range(n) if s[i]%p == 0)))

  exit(0) 

ans = g = 0

d = [0]*p

d[0] = 1

t = 1

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

    g = (g + s[i]*t)%p

    ans += d[g]

    d[g] += 1

    t = (t*10)%p

print(ans)
import sys

stdin = sys.stdin
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: list(map(int, stdin.readline().split()))
nl = lambda: list(map(int, stdin.readline().split()))
n, p = nm()
s = list(map(int, list(ns())))
ans = 0
if p == 2:
    for i in range(n):
        if s[i] % 2 == 0:
            ans += i + 1
    print(ans)
    exit(0)
if p == 5:
    for i in range(n):
        if s[i] % 5 == 0:
            ans += i + 1
    print(ans)
    exit(0)
g = [0] * (n + 1)
d = [0] * p
d[0] = 1
t = 1
for i in range(n - 1, -1, -1):
    g[i] = (g[i + 1] + s[i] * t) % p
    ans += d[g[i]]
    d[g[i]] += 1
    t = (t * 10) % p
print(ans)

n, p = list(map(int, input().split()))
s = list(map(int, list(eval(input()))))
if p in (2, 5):
    print((sum(i + 1 for i in range(n) if s[i] % p == 0)))
    exit(0)
ans = g = 0
d = [0] * p
d[0] = 1
t = 1
for i in range(n - 1, -1, -1):
    g = (g + s[i] * t) % p
    ans += d[g]
    d[g] += 1
    t = (t * 10) % p
print(ans)

-import sys - -stdin = sys.stdin -ns = lambda: stdin.readline().rstrip() -ni = lambda: int(stdin.readline().rstrip()) -nm = lambda: list(map(int, stdin.readline().split())) -nl = lambda: list(map(int, stdin.readline().split())) -n, p = nm() -s = list(map(int, list(ns()))) -ans = 0 -if p == 2: -    for i in range(n): -        if s[i] % 2 == 0: -            ans += i + 1 -    print(ans) +n, p = list(map(int, input().split())) +s = list(map(int, list(eval(input())))) +if p in (2, 5): +    print((sum(i + 1 for i in range(n) if s[i] % p == 0))) -if p == 5: -    for i in range(n): -        if s[i] % 5 == 0: -            ans += i + 1 -    print(ans) -    exit(0) -g = [0] * (n + 1) +ans = g = 0 -    g[i] = (g[i + 1] + s[i] * t) % p -    ans += d[g[i]] -    d[g[i]] += 1 +    g = (g + s[i] * t) % p +    ans += d[g] +    d[g] += 1