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

int N, P;
string S;

void solve(){

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


  unordered_map<int, int> rem;
  rem[0] = 1;
  int now = 0, r = 1;

  for(int i = N - 1; i >= 0; i--){
    now += ((S[i] - '0') * r) % P;
    now %= P;
    ans += rem[now]++;
    r = (r * 10) % P;
  }

  cout << ans << endl;
}


int main() {

  cin >> N >> P >> S;
  
  solve();
}