#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

struct RuntimeModInt {
public:
	using value_type = long long;
	
	RuntimeModInt(value_type val = 0) : val(val < 0 ? (mod() - (-val % mod())) % mod() : val % mod()) {}
	
	explicit operator bool() const noexcept { return val; }
	bool operator ==(const RuntimeModInt & rhs) const noexcept { return val == rhs.val; }
	bool operator !=(const RuntimeModInt & rhs) const noexcept { return !(*this == rhs); }
	RuntimeModInt operator +() const noexcept { return RuntimeModInt(*this); }
	RuntimeModInt operator -() const noexcept { return RuntimeModInt(0) -= *this; }
	RuntimeModInt operator +(const RuntimeModInt & rhs) const noexcept { return RuntimeModInt(*this) += rhs; }
	RuntimeModInt operator -(const RuntimeModInt & rhs) const noexcept { return RuntimeModInt(*this) -= rhs; }
	RuntimeModInt operator *(const RuntimeModInt & rhs) const noexcept { return RuntimeModInt(*this) *= rhs; }
	RuntimeModInt operator /(const RuntimeModInt & rhs) const noexcept { return RuntimeModInt(*this) /= rhs; }
	
	RuntimeModInt & operator +=(const RuntimeModInt & rhs) noexcept {
		val += rhs.val;
		if (val >= mod()) val -= mod();
		return *this;
	}
	RuntimeModInt & operator -=(const RuntimeModInt & rhs) noexcept {
		if (val < rhs.val) val += mod();
		val -= rhs.val;
		return *this;
	}
	RuntimeModInt & operator *=(const RuntimeModInt & rhs) noexcept {
		val = val * rhs.val % mod();
		return *this;
	}
	RuntimeModInt & operator /=(const RuntimeModInt & rhs) noexcept {
		*this *= rhs.inverse();
		return *this;
	}
	
	RuntimeModInt pow(value_type n) const {
		RuntimeModInt res = 1, x = val;
		if (n < 0) { x = x.inverse(); n = -n; }
		while (n) { if (n & 1) res *= x; x *= x; n >>= 1; }
		return res;
	}
	
	RuntimeModInt inverse() const {
		long long a = val, a1 = 1, a2 = 0, b = mod(), b1 = 0, b2 = 1;
		while (b > 0) {
			value_type q = a / b, r = a % b;
			value_type nb1 = a1 - q * b1, nb2 = a2 - q * b2;
			a = b; b = r;
			a1 = b1; b1 = nb1;
			a2 = b2; b2 = nb2;
		}
		assert(a == 1);
		return a1;
	}
	
	const value_type & get() const noexcept { return val; }
	static void set_mod(int M) noexcept { mod() = M; }
	static int get_mod() noexcept { return mod(); }
	
	friend std::ostream & operator <<(std::ostream & os, const RuntimeModInt & rhs) { return os << rhs.val; }
	friend std::istream & operator >>(std::istream & is, RuntimeModInt & rhs) {
		value_type x;
		is >> x;
		rhs = RuntimeModInt(x);
		return is;
	}
private:
	value_type val;
	
	static int & mod() {
		static int M = 2;
		return M;
	}
};
using mint = RuntimeModInt;


int N, P;
string S;

int main() {
	cin >> N >> P;
	cin >> S;
	REP(i, S.size()) S[i] -= '0';
	
	ll ans = 0;
	if (P == 2 || P == 5) {
		ans = (ll)N * (N + 1) / 2;
		REP(i, N) if (S[i] % P != 0) ans -= i + 1;
	}
	else {
		map<int, int> mm;
		mint::set_mod(P);
		
		mint cur = 0, p = 1;
		++mm[0];
		for (int i = N - 1; i >= 0; --i) {
			cur += p * S[i];
			p *= 10;
			ans += mm[cur.get()];
			++mm[cur.get()];
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 900