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;
  foreach (c; s[0 .. n]) {
    const int k = c.to!int - 48;
    b[] = 0;
    a[0] += 1;
    foreach (i, v; a) if (v) {
      b[((cast(int) i) * 10 + k) % p] += v;
    }
    res += b[0];
    swap (a, b);
  }
  writeln (res);
}

