import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.format;

void main() {
  string s = readln;
  int n, p;
  formattedRead (s, " %d %d", &n, &p);
  s = readln.stripRight;
  long res;
  auto a = new int[p];
  auto b = new int[p];
  a[0] = 0;
  auto t = new int[][](10, p);
  foreach (j; 0 .. 10) foreach (i; 0 .. p) {
    t[j][i] = (10 * i + j) % p;
  }
  if (p < 10) {
    foreach (c; s[0 .. n]) {
      const int k = c.to!int - 48;
      auto w = t[k];
      b[] = 0;
      a[0] += 1;
      foreach (i, v; a) {
        b[w[i]] += v;
      }
      res += b[0];
      swap (a, b);
    }
  } else {
    int pos = 0;
    foreach (c; s[0 .. n]) {
      const int k = c.to!int - 48;
      auto w = t[k];
      a[pos] += 1;
      pos = w[pos];
      /*
      foreach (i, v; a) {
        b[w[i]] = v;
      }
      */
      res += a[pos];
      //swap (a, b);
    }
  }
  writeln (res);
}

