import java.util.Scanner


fun main(args: Array<String>) {
	val scn = Scanner(System.`in`)
	val n = scn.nextInt()
	val p = scn.nextInt()
	val s = scn.next().toCharArray().map{Integer.parseInt(it.toString())}
	var ans = 0L
	if (p==2 || p==5){
		s.forEachIndexed {i, d -> if (d%p==0) ans += i+1 }
	}else {
		val map = mutableMapOf<Int, Long>()
		var crt = s.last()%p
		var k = 1
		map[crt] = 1
		for (i in s.lastIndex-1 downTo 0){
			k = (k*10)%p
			crt = (k*s[i]+crt)%p
			map[crt] = map.getOrElse(crt){0} + 1
		}
		map.values.forEach { ans += it*(it-1)/2 }
		if (map.containsKey(0)){
			ans += map[0]!!
		}
	}
	print(ans)
}