local n, p = io.read("*n", "*n", "*l")
local str = io.read()
if p == 2 then
  local ret = 0
  for i = n, 1, - 1 do
    local num = tonumber(str:sub(i, i))
    if num % 2 == 0 then
      ret = ret + i
    end
  end
  print(ret)
elseif p == 5 then
  local ret = 0
  for i = n, 1, - 1 do
    local num = tonumber(str:sub(i, i))
    if num % 5 == 0 then
      ret = ret + i
    end
  end
  print(ret)
else
  local t = {}
  for i = 1, p do
    t[i] = 0
  end
  t[1] = 1
  local mul = 1
  local cur = 0
  local ret = 0
  for i = n, 1, - 1 do
    local num = tonumber(str:sub(i, i))
    cur = (cur + mul * num) % p
    ret = ret + t[cur + 1]
    t[cur + 1] = t[cur + 1] + 1
    mul = (mul * 10) % p
  end
  print(ret)
end
