import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.{Map, Stack, Queue, PriorityQueue, Set}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N, P = in.next().toInt
  val S = in.next()

  //
  if (P == 2 || P == 5) {
    var ans = 0L
    for (i <- (0 until N).reverse) {
      val c = S(i)
      val n = c.toString.toInt

      if (n % P == 0)
        ans += i + 1
    }

    println(ans)
  }
  else {
    val dp = new Array[Long](N+1)
    var base = 1L
    var acc = 0L
    for (i <- (0 until N).reverse) {
      val c = S(i)
      val n = c.toString.toInt

      acc += n * base
      acc %= P

      dp(i) = acc

      base *= 10
      base %= P
    }

    val hash = Map[Long, Int]()
    for (i <- 0 to N) {
      val now = dp(i)
      if (!hash.contains(now))
        hash += (now -> 1)
      else
        hash(now) += 1
    }

    var ans = 0L
    for ((_, v) <- hash) {
      ans += (v * (v - 1)) / 2
    }

    println(ans)
  }
}

object Sub {
  def print2DimArray[A](mat: Array[Array[A]]): Unit = {
    mat.foreach(lin => println(lin.toList))
  }
}

class InputReader(stream: InputStream) {
  private[this] val reader = new BufferedReader(new InputStreamReader(stream))
  private[this] var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
