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

int n, p;
string s;
int main() {
  cin >> n >> p >> s;
  if (p == 2 || p == 5) {
    long sum = 0;
    for (int i = 0; i < n; ++i) {
      if ((s[i] - '0') % p == 0) {
        sum += i + 1;
      }
    }
    cout << sum << endl;
  } else {
    long sum = 0;
    map<int, int> mp;
    ++mp[0];
    long now = 0;
    long base = 1;
    for (int i = n - 1; i >= 0; --i) {
      now = (now + (s[i] - '0') * base) % p;
      base = base * 10 % p;
      sum += mp[now];
      ++mp[now];
    }
    cout << sum << endl;
  }
}
