program divisible_substring
  implicit none
  integer :: n, p, i, m, d = 0, k, l
  character(200000) :: s
  integer(8) :: cnt(0:10000) = 0, ans = 0
  read(*,*) n, p
  read(*,*) s
  k = inv(10)
  do i = 1, n
    m = mod(ichar(s(i:i))-48,p)
    l = mod(d+m,p)
    cnt(l) = cnt(l)+1
    d = modulo(d-m*k,p)
    ans = ans+cnt(d)
  end do
  write(*,'(i0)') ans
contains
  function inv(n) result(y)
    integer, intent(in) :: n
    integer :: a, b, x, y, t, q
    a = modulo(n,p)
    b = p
    x = 0
    y = 1
    do while (b /= 0)
      q = a/b
      t = b
      b = mod(a,b)
      a = t
      t = y
      y = x
      x = t-q*x
    end do
    y = modulo(y,p)
  end
end program divisible_substring