#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long cnt[10010];
    long long ans = 0;
    string s;
    int n,p;
    
    cin >> n >> p >> s;
    
    if(p == 2 || p == 5)
    {
        for(int i = 0;i < n;i++) 
            if((s[i] - '0') % p == 0) 
                ans += i + 1;
    }
    else
    {
        cnt[0] = 1;
        long long cur = 0,t = 1;
        for(int i = n - 1;i >= 0;i--)
        {
            cur = (cur + (s[i] - '0')*t%p) % p;
            t = t*10%p;
            ans += cnt[cur]++;
        }
    }
    
    cout << ans << endl;
    return 0;
}