#include <bits/stdc++.h>
using namespace std;

int main() {
	int N, P;
	string S;
	cin >> N >> P >> S;
	long long ans = 0;

	if (P == 2 or P == 5) {
		for (int i = 0; i < N; i++) {
			int d = S[i] - '0';
			if (d % P == 0) {
				ans += i + 1;
			}
		}
	} else {
		vector<int> num(P);
		int base = 1, sum = 0;
		num[0] = 1;
		for (int i = N - 1; i >= 0; i--) {
			int d = S[i] - '0';
			sum = (sum + base * d) % P;
			ans += num[sum];
			num[sum]++;
			base = base * 10 % P;
		}
	}

	cout << ans << endl;

	return 0;
}