#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e18

class ModInt {
    using u64 = std::uint_fast64_t;
    static u64 &mod() {
        static u64 mod_ = 0;
        return mod_;
    }

   public:
    u64 x;
    ModInt(const ll x = 0) : x(x < 0 ? (mod() - (-x % mod())) % mod() : x % mod()) {}

    ModInt operator+(const ModInt r) { return ModInt(*this) += r; }
    ModInt operator*(const ModInt r) { return ModInt(*this) *= r; }
    ModInt operator-(const ModInt r) { return ModInt(*this) -= r; }
    ModInt operator/(const ModInt r) { return ModInt(*this) /= r; }
    ModInt operator-() { return ModInt(mod() - x); }

    ModInt &operator+=(const ModInt r) {
        x += r.x;
        if (x >= mod()) x -= mod();
        return *this;
    }
    ModInt &operator-=(const ModInt r) {
        if (x < r.x) x += mod();
        x -= r.x;
        return *this;
    }
    ModInt &operator*=(const ModInt r) {
        x *= r.x;
        if (x >= mod()) x %= mod();
        return *this;
    }
    ModInt &operator/=(ModInt r) {
        if (!(x % r.x)) {
            x /= r.x;
            return *this;
        }
        u64 p = mod() - 2;
        while (p > 0) {
            if (p & 1) *this *= r;
            r *= r;
            p >>= 1;
        }
        return *this;
    }
    ModInt &operator++(int) { return (*this) += 1; }
    ModInt &operator++() { return (*this) += 1; }
    ModInt &operator--(int) { return (*this) -= 1; }
    ModInt &operator--() { return (*this) -= 1; }

    bool operator<(const ModInt r) { return x < r.x; }
    bool operator>(const ModInt r) { return x > r.x; }
    bool operator<=(const ModInt r) { return x <= r.x; }
    bool operator>=(const ModInt r) { return x >= r.x; }
    bool operator==(const ModInt r) { return x == r.x; }
    bool operator!=(const ModInt r) { return x != r.x; }

    ModInt inv() { return (ModInt)1 / (*this); }
    int get_mod() { return mod(); }

    friend std::istream &operator>>(std::istream &in, ModInt &m) {
        ll a;
        in >> a;
        if (a < 0) a = mod() - (-a % mod());
        if (a >= mod()) a %= mod();
        m.x = a;
        return in;
    }
    friend std::ostream &operator<<(std::ostream &out, const ModInt &m) {
        out << m.x;
        return out;
    }

    static void set_mod(const u64 m) { mod() = m; }
};
using mint = ModInt;

template <typename T, typename U>
T pow(T x, U n, T e = 1) {
    T res = e;
    while (n > 0) {
        if (n & 1) res *= x;
        x *= x;
        n >>= 1;
    }
    return res;
}

int main() {
    int N, P;
    string S;
    cin >> N >> P >> S;
    S = S + "0";
    reverse(S.begin(), S.end());
    ll ans = 0;

    if (P == 2) {
        for (int i = 1; i <= N; i++)
            if ((S[i] - '0') % 2 == 0) ans += N - i + 1;
        cout << ans << endl;
        return 0;
    }
    if (P == 5) {
        for (int i = 1; i <= N; i++)
            if ((S[i] - '0') % 5 == 0) ans += N - i + 1;
        cout << ans << endl;
        return 0;
    }

    ModInt::set_mod(P);
    vector<mint> s(N + 1, 0);
    map<int, ll> mp;
    mp[0] = 1;
    for (int i = 1; i <= N; i++) {
        s[i] = s[i - 1] + pow<mint, int>(10, i - 1) * (S[i] - '0');
        mp[s[i].x]++;
    }
    for (auto &p : mp) {
        ans += p.second * (p.second - 1) / 2;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 2624