#include <iostream>
#include <string>
#include <string.h>
using namespace std;

string s;
long long n, p, t = 0, m = 0, w = 1;
long long a[10000];

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

	if (p == 2 || p == 5) {
		for (long long i = 0; i < n; i++) {
			t = s[i] - '0';

			if (t % p == 0)
				m += i + 1;
		}

		cout << m << endl;
	}
	else {
		memset(a, 0, sizeof(a));

		for (long long i = 0; i < n; i++) {
			t = (t + (s[n - 1 - i] - '0') * w) % p;
			w = w * 10 % p;
			a[t]++;
		}

		m = a[0] * (a[0] + 1) / 2;

		for (long long i = 1; i < p; i++)
			m += a[i] * (a[i] - 1) / 2;

		cout << m << endl;
	}
}
