#include <bits/stdc++.h>
#define int long long
#define N (int) 2e5
using namespace std;
int n, p;
string s;

int barket[N];

signed main () {
	cin >> n >> p >> s;

	if (p == 2 || p == 5) {
		int ans = 0;
		for (int i = 0; i < n; i ++) {
			int tmp = s[i] - '0';
			if (tmp % p == 0) ans += i+1;
		}
		cout << ans << endl;
	} else {
		int sum = 0, t = 1;
		int ans = 0;
		barket[0] = 1;
		for (int i = n-1; i >= 0; i --) {
			sum = ((s[i]-'0') * t + sum) % p;
			(t *= 10) %= p;
			ans += barket[sum] ++;
//			cout << sum << endl;
		}
		cout << ans << endl;
	}
	
	return 0;
}