#include <bits/stdc++.h>

int main() {
  int n,p;
  std::string s;
  std::cin >> n >> p >> s;

  std::uint64_t counter = 0;
  if (p == 2 || p == 5) {
    for (int i = 0; i < n; ++i) {
      int a = s[i] - '0';
      if ((a % p) == 0) {
        counter += i + 1;
      }
    }
  } else {
    int mod[10000] = {1};
    int p10 = 1;
    int subs = 0;
    for (int i = n - 1; i >= 0; --i) {
      subs = (subs + (s[i] - '0') * p10) % p;
      counter += mod[subs];
      mod[subs]++;
      p10 = (p10 * 10) % p;
    }
  }

  std::cout << counter << std::endl;

  return 0;
}
