#include <bits/stdc++.h>
long A,n,p,t,a[10000],L,D=1;std::string s;int main(){std::cin>>n>>p>>s;for(int i = n - 1; i >= 0 ; i--){L=s[i]-'0',t=(D*L+t)%p,D=(D*10)%p;A+=((10%p)!=0)?(t==0)+a[t]++:((L%p)==0)*(i+1);}std::cout<<A;}
