#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww
{
    cww()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
    }
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts)
{
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v)
{
    for (auto &e : t)
        fill_v(e, v);
}
struct ArbitraryModInt
{

    int x;

    ArbitraryModInt() : x(0) {}

    ArbitraryModInt(int64_t y) : x(y >= 0 ? y % mod() : (mod() - (-y) % mod()) % mod()) {}

    static int &mod()
    {
        static int mod = 0;
        return mod;
    }

    static int set_mod(int md)
    {
        mod() = md;
    }

    ArbitraryModInt &operator+=(const ArbitraryModInt &p)
    {
        if ((x += p.x) >= mod())
            x -= mod();
        return *this;
    }

    ArbitraryModInt &operator-=(const ArbitraryModInt &p)
    {
        if ((x += mod() - p.x) >= mod())
            x -= mod();
        return *this;
    }

    ArbitraryModInt &operator*=(const ArbitraryModInt &p)
    {
        unsigned long long a = (unsigned long long)x * p.x;
        unsigned xh = (unsigned)(a >> 32), xl = (unsigned)a, d, m;
        asm("divl %4; \n\t"
            : "=a"(d), "=d"(m)
            : "d"(xh), "a"(xl), "r"(mod()));
        x = m;
        return *this;
    }

    ArbitraryModInt &operator/=(const ArbitraryModInt &p)
    {
        *this *= p.inverse();
        return *this;
    }

    ArbitraryModInt operator-() const { return ArbitraryModInt(-x); }

    ArbitraryModInt operator+(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) += p; }

    ArbitraryModInt operator-(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) -= p; }

    ArbitraryModInt operator*(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) *= p; }

    ArbitraryModInt operator/(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) /= p; }

    bool operator==(const ArbitraryModInt &p) const { return x == p.x; }

    bool operator!=(const ArbitraryModInt &p) const { return x != p.x; }

    ArbitraryModInt inverse() const
    {
        int a = x, b = mod(), u = 1, v = 0, t;
        while (b > 0)
        {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ArbitraryModInt(u);
    }

    ArbitraryModInt pow(int64_t n) const
    {
        ArbitraryModInt ret(1), mul(x);
        while (n > 0)
        {
            if (n & 1)
                ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const ArbitraryModInt &p)
    {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, ArbitraryModInt &a)
    {
        int64_t t;
        is >> t;
        a = ArbitraryModInt(t);
        return (is);
    }
};
int N, P;
string S;

int main()
{
    cin >> N >> P;
    cin >> S;
    ll ans = 0;
    if (P != 2 and P != 5)
    {
        ArbitraryModInt::set_mod(P);
        using mint = ArbitraryModInt;

        vector<mint> U(N + 1);
        for (int i = N; i > 0; i--)
        {
            U[i - 1] = U[i] + mint(S[i - 1] - '0') * mint(10).pow(N - i);
        }
        unordered_map<ll, ll> cnt;
        rep(i, N + 1)
        {
            cnt[U[i].x]++;
        }

        for (auto c : cnt)
        {
            ll j = c.second;
            ans += j * (j - 1) / 2;
        }
    }
    else
    {
        if (P == 2)
        {
            rep(i, N)
            {
                if ((S[i] - '0') % 2 == 0)
                {
                    ans += i + 1;
                }
            }
        }
        else if (P == 5)
        {
            rep(i, N)
            {
                if ((S[i] - '0') % 5 == 0)
                {
                    ans += i + 1;
                }
            }
        }
    }
    cout << ans;
    return 0;
}

Measured memory_bytes: 1684