import java.io.{BufferedOutputStream, BufferedReader, InputStreamReader, PrintWriter}

object Main {
  def main(args: Array[String]): Unit = {
    val in = new BufferedReader(new InputStreamReader(System.in))
    val out = new PrintWriter(new BufferedOutputStream(System.out))
    val Array(n, p) = in.readLine().split("\\s+").map(_.toInt)
    val s = in.readLine().toCharArray.reverse.map(_.toInt - '0')

    if (p == 2 || p == 5) {
      var count = 0L
      for {
        i <- s.indices
      } {
        if (s(i) % p == 0) {
          count += n - i
        }
      }
      out.println(count)
    } else {
      def getRemainders() = {
        val remainders = Array.ofDim[Int](n)

        var base = 1
        for {
          i <- s.indices
        } {
          remainders(i) = base * s(i) % p
          base = base * 10 % p
        }

        remainders
      }

      val m = getRemainders()

      def getSumOfRemainders() = {
        val sumOfRemainders = Array.ofDim[Int](n + 1)

        sumOfRemainders(0) = 0
        for {
          i <- m.indices
        } {
          sumOfRemainders(i + 1) = (sumOfRemainders(i) + m(i)) % p
        }

        sumOfRemainders
      }

      val sm = getSumOfRemainders()

      val remainders = Array.ofDim[Int](10001)
      for {
        asm <- sm
      } {
        remainders(asm) += 1
      }

      var count = 0L
      for {
        r <- remainders
        if r > 1
      } {
        count += r * (r - 1) / 2
      }

      out.println(count)
    }


    out.flush()
  }
}
