#include <bits/stdc++.h>
int N, P, x;
long long ans;
char S[200005];
int m[10005];
void test() {
	std::cin >> S + 1;
	long long ans = 0;
	for (int i = 1; i <= N; ++i) {
		int x = S[i] & 15;
		if (x % P == 0) ans += i;
	}
	std::cout << ans << '\n';
}
int main() {
	std::cin >> N >> P;
	if (P == 2 || P == 5) return test(), 0;
	std::cin >> S + 1;
	m[0] = 1;
	int c = 1;
	std::reverse(S + 1, S + N + 1);
	for (int i = 1; i <= N; ++i) {
		x = x + c * (S[i] & 15), x %= P, c = c * 10 % P;
		ans += m[x]; ++m[x];
	}
	std::cout << ans << '\n';
}