#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

int main() {
  ll n,p;
  string s;
  cin >> n >> p >> s;
  ll ans=0;
  if(p==2||p==5) {
    for(ll i=0;i<n;i++) {
      if(int(s[i]-'0')%p==0) {
        ans+=i+1;
      }
    }
    cout << ans << endl;
    return 0;
  }
  ll temp=1;
  ll sum=0;
  map<ll,ll> count;
  for(ll i=0;i<n;i++) {
    sum=(sum+(int(s[n-1-i]-'0')*temp)%p)%p;
    count[sum]++;
    temp=temp*10%p;
  }
  for(auto q:count) {
    ans+=q.second*(q.second-1)/2;
    if(q.first==0) {
      ans+=q.second;
    }
  }
  cout << ans << endl;
}