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

struct ModInt {
  static int32_t Mod;
  using ModType = int32_t;
  template<typename T> ModType modify(T n) const {
    if(n < 0 || n >= Mod) { n %= Mod; }
    if(n < 0) { n += Mod; }
    return static_cast<ModType>(n);
  } 
  template<typename T>
  ModInt pow(T p) const {
    ModInt res {1}, a = *this;
    do {
      if(p & 1) { res *= a; }
      a *= a;
    } while(p >>= 1);
    return res;
  }
  ModInt inv() const {
    return pow(Mod - 2);
  }
 
  ModInt() = default;
  ModInt(const ModInt& other) { val = other.val; }
  ModInt(ModInt&& other) { val = other.val; }
  ModInt& operator=(const ModInt& other) { this->val = other.val; return *this; }
  ModInt& operator=(const ModInt&& other) { this->val = other.val; return *this; }
  template<typename T> ModInt(T n) { val = modify(n); }
 
  ModInt& operator+=(const ModInt& other) { if((val += other.val) >= Mod) val -= Mod; return *this; }
  ModInt& operator-=(const ModInt& other) { if((val -= other.val) < 0) val += Mod; return *this; }
  ModInt& operator*=(const ModInt& other) { val = modify((int64_t) (val) * (int64_t)(other.val)); return *this; }
  ModInt& operator/=(const ModInt& other) { return *this *= other.inv(); }
 
  ModInt& operator++() { return *this += 1; }
  ModInt& operator--() { return *this -= 1; }
  ModInt operator++(int) { ModInt res{*this}; *this += 1; return res; }
  ModInt operator--(int) { ModInt res{*this}; *this -= 1; return res; }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { return ModInt{Mod - val}; }
 
  ModInt operator+(const ModInt& other) const { return ModInt(*this) += other; }
  ModInt operator-(const ModInt& other) const { return ModInt(*this) -= other; }
  ModInt operator*(const ModInt& other) const { return ModInt(*this) *= other; }
  ModInt operator/(const ModInt& other) const { return ModInt(*this) /= other; }
  ModInt operator+(const ModInt&& other) const { return ModInt(*this) += other; }
  ModInt operator-(const ModInt&& other) const { return ModInt(*this) -= other; }
  ModInt operator*(const ModInt&& other) const { return ModInt(*this) *= other; }
  ModInt operator/(const ModInt&& other) const { return ModInt(*this) /= other; }
 
  template<typename T> ModInt operator+(T other) const { return *this + ModInt(other); }
  template<typename T> ModInt operator-(T other) const { return *this - ModInt(other); }
  template<typename T> ModInt operator*(T other) const { return *this * ModInt(other); }
  template<typename T> ModInt operator/(T other) const { return *this / ModInt(other); }
  template<typename T> ModInt& operator+=(T other) { return *this += ModInt(other); }
  template<typename T> ModInt& operator-=(T other) { return *this -= ModInt(other); }
  template<typename T> ModInt& operator*=(T other) { return *this *= ModInt(other); }
  template<typename T> ModInt& operator/=(T other) { return *this /= ModInt(other); }
 
  friend ostream& operator<< (ostream& os, const ModInt& m) { return (os << m.val); }

 
  explicit operator ModType() { return val; }
private:
  ModType val = 0;
};

int32_t ModInt::Mod = 0;

using Mint = ModInt;

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n, md;
  string s;
  cin >> n >> md >> s;
  ModInt::Mod = md;
  if(Mint::Mod == 2 || Mint::Mod == 5) {
    long long ans = 0;
    for(int i = 0; i < n; ++i) {
      if((s[i] - '0') % Mint::Mod == 0) {
        ans += i + 1;
      }
    }
    cout << ans << '\n';
    return 0;
  }
  vector<int> cnt(Mint::Mod);
  Mint pref = 0, p = 1;
  for(int i = 0; i < n; ++i, p *= 10) {
    pref = pref * 10 + (s[i] - '0');
    cnt[(int)(pref * p.inv())] += 1;
  }
  long long ans = 0;
  for(int i = 0; i < Mint::Mod; ++i) {
    ans += 1LL * cnt[i] * (cnt[i] - 1) / 2;
    if(i == 0) ans += cnt[i];
  }
  cout << ans << '\n';
}

Measured memory_bytes: 848