program divisible_substring
  implicit none
  integer :: n, p, i, j, d, k = 0, cnt(0:10000) = 0, hash = 0, pow = 1
  character(200000) :: s
  integer(8) :: x = 0
  read(*,*) n, p
  read(*,*) s
  if (p == 2 .or. p == 5) then
    do i = n, 1, -1
      d = ichar(s(i:i)) - 48
      if (mod(d, p) == 0) then
        x = x + i
      end if
    end do
    write(*,'(i0)') x
    stop
  end if
  do i = n, 1, -1
    d = ichar(s(i:i)) - 48
    hash = mod(d * pow + hash, p)
    x = x + cnt(hash)
    cnt(hash) = cnt(hash) + 1
    pow = mod(10 * pow, p)
  end do
  write(*,'(i0)') x + p
end program divisible_substring