import java.util.*

fun main(args: Array<String>) {
    val pw = java.io.PrintWriter(System.out)
    Problem.solve(Scanner(System.`in`), pw)
    pw.flush()
}

object Problem {
    fun <A> countList(a: List<A>): Map<A, Long> {
        val result = mutableMapOf<A, Long>()
        a.forEach {
            result[it] = (result[it] ?: 0L) + 1L
        }
        return result
    }

    fun solve(sc: Scanner, pw: java.io.PrintWriter) {
        val N = sc.next().toInt()
        val P = sc.next().toInt()
        val S = sc.next().map { it - '0' }.toTypedArray()

        var ans = 0L
        if (10 % P == 0) {
            for (i in S.indices) {
                if (S[i] % P == 0) {
                    ans += i + 1
                }
            }
        } else {
            var d = 1
            val R = S.indices.map { i ->
                val r = S[N - 1 - i] * d % P
                d *= 10
                d %= P
                r
            }
            val R2 = mutableListOf(0)
            var i = 0
            R.forEach {
                i += it
                i %= P
                R2.add(i)
            }
            countList(R2).forEach {
                val n = it.value
                ans += (n * (n - 1) / 2)
            }
        }
        pw.println(ans)
    }
}
