#include <bits/stdc++.h>
using LL = long long;
using namespace std;
int main() {
	int n, p; cin >> n >> p;
	string s; cin >> s;
	LL ans = 0;
	if (p == 2 or p == 5) {
		for (int i = 0; i < n; i++) 
			if ((s[i] - '0') % p == 0) ans += i + 1;
	} else {
		map<int, int> mp;
		mp[0] = 1;
		LL sum = 0, power = 1;
		for (int i = n - 1; i >= 0; i--) {
			sum = (sum + (s[i] - '0') * power) % p;
			ans += mp[sum % p]++;
			power = power * 10 % p;
		}
	}
	cout << ans;	
}