program main
  implicit none
  integer(8) :: n, m, i, dig, res
  integer(8), allocatable :: d(:), cnt(:)
  character(200000) :: s
  read *, n, m
  read *, s
  allocate (d(n), cnt(0:m - 1))

  if (m == 2 .or. m == 5) then
    res = 0
    do i = 1, n
      read (s(i:i), *) dig
      if (modulo(dig, m) == 0) res = res + i
    end do
  else
    read (s(n:n), *) dig
    cnt = 0
    cnt(0) = 1
    d(n) = modulo(dig, m)
    cnt(d(n)) = cnt(d(n)) + 1
    do i = n - 1, 1, -1
      read (s(i:i), *) dig
      d(i) = d(i + 1) + pow(10_8, n - i) * dig
      d(i) = modulo(d(i), m)
      cnt(d(i)) = cnt(d(i)) + 1
    end do

    res = 0
    do i = 0, m - 1
      res = res + cnt(i) * (cnt(i) - 1) / 2
    end do
  end if
  print "(i0)", res
  contains

  recursive function pow(n, p) result(res)
    integer(8), intent(in) :: n, p
    integer(8) :: res
    if (p == 0) then
      res = 1
    else if (modulo(p, 2_8) == 0) then
      res = modulo(pow(n, p / 2) ** 2, m)
    else
      res = modulo(pow(n, p - 1) * n, m)
    end if
  end function pow
end program main
