import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

long calc(int p, string s) {
    if (p == 2 || p == 5) {
        long ans = 0;
        for (int i = cast(int)s.length-1; i >= 0; i--) {
            if ((s[i] - '0') % p == 0) {
                // 下一桁が p で割り切れるので、区間 [0, i] は全て p で割り切れる
                ans += i + 1;
            }
        }
        return ans;
    }

    long[int] d = [0: 1];
    int x = 0;
    int t = 1;
    foreach_reverse (c; s) {
        x = (x + (c - '0') * t) % p;
        t = 10 * t % p;
        d[x]++;
    }
    return d.values.map!(e => e * (e - 1) / 2).sum;
}

void main() {
    int n, p; scan(n, p);
    string s = read!string;
    writeln(calc(p, s));
}

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
