import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;

enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;



void main() {
    int N, p;
    scan(N, p);
    string s;
    scan(s);
    auto a = s.map!(ch => ch - '0').array;

    long ans;

    if (p == 2) {
        foreach (i ; 0 .. N) {
            if (a[i] % 2 == 0) {
                ans += i + 1;
            }
        }
    }
    else if (p == 5) {
        foreach (i ; 0 .. N) {
            if (a[i] == 0 || a[i] == 5) {
                ans += i + 1;
            }
        }
    }
    else {
        auto r = new int[](N + 1);
        foreach (i ; 1 .. N + 1) {
            r[i] = r[i - 1] * 10 % p + a[i - 1] % p;
            r[i] %= p;
        }

        auto rt = powmod(10, p - 2, p);

        foreach (i ; 0 .. N + 1) {
            r[i] = r[i] * powmod(rt, i, p) % p;
        }

        auto cnt = new long[](p);
        foreach (i ; 0 .. N + 1) {
            ans += cnt[r[i]];
            cnt[r[i]]++;
            debug {
                writeln(cnt);
            }
        }
    }

    writeln(ans);
}

long powmod(long x, long y, long p) {
    return y > 0 ? powmod(x, y >> 1, p)^^2 % p * x^^(y & 1) % p : 1L;
}


void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x > arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x < arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}



