import io.StdIn._

object Main extends App {
  val line1 = readLine.split(" ").map(_.toInt)
  val N = line1(0)
  val P = line1(1)

  val S = readLine.toCharArray.map(_.toInt - 48)

  if (10 % P == 0) {
    var ans = 0L
    for (i <- 0 until N) {
      if (S(i) % P == 0) {
        ans = ans + i + 1
      }
    }
    println(ans)
  } else {

    val d = Array.fill[Int](N + 1)(0)
    var ten = 1
    for (i <- N - 1 to 0 by -1) {
      val a = S(i) * ten % P
      d(i) = (d(i + 1) + a) % P
      ten = ten * 10 % P
    }

    val cnt = Array.fill[Int](P)(0)
    var ans = 0L
    for (i <- N to 0 by -1) {
      ans = ans + cnt(d(i))
      cnt(d(i)) = cnt(d(i)) + 1
    }

    println(ans)
  }
}
