fun main(args: Array<String>) {
    val (N, P) = listOfInt()
    val S = next().map { it - '0' }
    var ans = 0L
    if (10 % P == 0) { //P = 2, 5
        repeat(N) { r ->
            if (S[r] % P == 0) ans += r + 1
        }
        println(ans)
        return
    }
    val d = IntArray(N + 1)
    var ten = 1
    for (n in (0 until N).reversed()) {
        val a = S[n] * ten % P
        d[n] = (d[n + 1] + a) % P
        ten *= 10; ten %= P
    }
    val c = IntArray(P)
    for (n in (0 .. N).reversed()) {
        ans += c[d[n]]
        c[d[n]] += 1
    }
    println(ans)
}
fun next() = readLine()!!
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map(String::toInt)
