fun main(args: Array<String>) {
    val (N, P) = readInputLine().split(" ").map { it.toInt() }
    val S = readInputLine().reversed().toCharArray()

    var ans = 0L

    if (P == 2 || P == 5) {
        for ((i, c) in S.withIndex()) {
            if ((c - '0') % P == 0) {
               ans += (N - i).toLong()
            }
        }
        println(ans)
        return
    }

    val modCnt = LongArray(P)
    modCnt[0]++

    var current = 0
    var digitFactor = 1
    for (c in S) {
        current = ((c - '0') * digitFactor + current) % P
        digitFactor = (digitFactor * 10) % P
        modCnt[current]++
    }

    repeat(P) {
        ans += modCnt[it] * (modCnt[it] - 1L) / 2L
    }

    println(ans)
}

fun readInputLine(): String {
    return readLine()!!
}
