import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container, std.format;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    long N, P;
    scan(N, P);
    auto S = sread();

    auto pow10 = new long[](N + 1);
    pow10[0] = 1;
    foreach (i; 0 .. N)
        pow10[i + 1] = pow10[i] * 10 % P;

    auto U = new long[](N + 1);
    foreach (i; 0 .. N)
    {
        U[N - (i + 1)] = (U[N - i] + ((S[N - i - 1] - '0') * pow10[i] % P)) % P;
    }

    auto cnt = new long[](P + 1);
    cnt[0] = 1;
    long ans;

    // writeln(U);
    foreach_reverse (i; 0 .. N)
    {
        long c = U[i];
        long m = (P + c) % P;
        bool a = (P != 2 && P != 5);
        bool b = !a && ((S[i] - '0') % P == 0);
        if (a || b)
            ans += cnt[m];
        // writeln(S[i .. $], ' ', cnt[m], cnt, ' ', ans);
        cnt[U[i]]++;
    }
    writeln(ans);
    // foreach (i; 0 .. N)
    //     foreach (j; i .. N)
    //         writeln((U[i] - U[j + 1]) / (10 ^^ (N - j - 1)));
}
