fun main(args: Array<String>) {
    val (n, p) = readLine()!!.split(' ').map { it.toInt() }
    val s = readLine()!!.reversed()

    val ans = if (10 % p == 0) {
        (0 until n)
                .filter { (s[it] - '0') % p == 0 }
                .map { 0L + n - it }
                .sum()
    } else {
        val cnt = IntArray(p)
        var x = 0
        var b = 1
        var ans = 0L
        for (i in 0 until n) {
            cnt[x]++
            x += (s[i] - '0') * b
            x %= p
            ans += cnt[x]
            b = b * 10 % p
        }
        ans

    }

    println(ans)
}
