fun main(args: Array<String>) {
    val (n, p) = readInts()

    val s = readLn()
    if (p == 2 || p == 5) {
        var ans = 0L
        for (i in 0 until n) {
            if ((s[i] - '0') % p == 0) ans += (i + 1).toLong()
        }
        println(ans)
        return
    }
    val modP = mutableListOf<Int>()
    var mul = 1
    for (i in s.length - 1 downTo 0) {
        val num = s[i] - '0'
        modP.add(((if (modP.isNotEmpty()) modP.last() else 0) + mul * num) % p)
        mul = (mul * 10) % p
    }

    val occurs = mutableMapOf<Int, Int>()
    occurs[0] = 1
    modP.forEach {
        occurs[it] = occurs[it]?.plus(1) ?: 1
    }
    var ans = 0L
    occurs.forEach { entry ->
        val occ = entry.value
        ans += (occ.toLong() * (occ - 1)) / 2
    }
    println(ans)
}

// Input Reader
private fun readLn() = readLine()!!

private fun readStrings() = readLn().trim().split(" ")
private fun readInts() = readStrings().map { it.toInt() }
