/// wa ^3^

#pragma GCC optimize("O3")

#include<bits/stdc++.h>
                           
#define x first
#define y second
#define pb push_back
#define sz(x) (int) x.size()
 
using namespace std;
 
using ll = long long;
using pii = pair<int, int>;                                   

const int INF32 = 1e9 + 1e2;

void stndrtn() {
#ifdef accepted    
    freopen(".in", "r", stdin);
#endif
}

int MOD;

int mult(int x, int y) {
    return (ll)(x * 1ll * y) % MOD;
}

int binpow(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1)
            res = mult(res, a);
        a = mult(a, a);
        b >>= 1;         
    }    
    return res;
}                                    

int dv(int a, int b) {
    int x = mult(a, binpow(b, MOD-2));
    return x + (-x+MOD-1)/MOD * MOD;
}

const int MAX = 1e5+10;

int cnt[MAX];

vector<int> a;

ll solve(int l, int r) {
    if (r-l == 1) 
        return a[l] % MOD == 0;
    int m = l+r >> 1;
    int ten = 1, pv = 0;
    for (int i = m-1; i >= l; --i) {
        pv = ((a[i]*ten + pv) % MOD);
        cnt[pv]++, ten = mult(ten, 10);        
    }
    ten = 1;
    ll sum = 0;
    int rd = 0;
    for (int i = m; i < r; ++i) {
        rd = (rd * 10 + a[i]) % MOD;
        sum += cnt[dv(-rd, ten = mult(ten, 10))];    
    }
    ten = 1, pv = 0;
    for (int i = m-1; i >= l; --i) {
        pv = ((a[i]*ten + pv) % MOD);
        cnt[pv]--, ten = mult(ten, 10);        
    }
    return sum + solve(l, m) + solve(m, r);
}

int brute(vector<int> a, bool tt = 0) {
    int res = 0;
    for (int l = 0; l < sz(a); ++l) {
        int rd = 0;
        for (int r = l; r < sz(a); ++r) {
            rd = (rd * 10 + a[r]) % MOD;
            if (tt && rd == 0)
                cout << l+1 << " " << r+1 << "\n";
            res += (rd == 0);
        }
    }
    return res;
}

uniform_int_distribution<int> d(1, 1000);
mt19937 rnd;

int main() {
    ios_base::sync_with_stdio(0);                               
    cin.tie(nullptr), stndrtn();
    rnd.seed(time(nullptr));
    int n;
    bool tt = 0;
    if (tt) {
        while (1) {
            n = d(rnd), MOD = 5;
            //cin >> n >> MOD;
            string s = "";
            //cin >> s;
            for (int i = 0; i < n; ++i)
                s += (d(rnd) % 10) + '0';
            a.clear();
            for (char c : s)
                a.pb(c-'0');
            int fast = solve(0, n);
            int slow = brute(a);  
            if (MOD == 2) {
                ll cnt = 0;
                for (int x : a) {
                    cnt++;
                    if (x % 2 == 0)
                        fast += cnt;
                }
            }
            if (fast != slow) {
                cout << s << "\n";
                cout << fast << " " << slow << "\n";
                brute(a, 1);
                return 0;
            }                 
        }
    } else {
        cin >> n >> MOD;
        string s;
        cin >> s;
        for (char c : s)
            a.pb(c-'0');
        ll cnt = 0, ans = 0;
        if (MOD == 2) {
            for (int x : a) {
                cnt++;
                if (x % 2 == 0)
                    ans += cnt;
            }
        } else if (MOD == 5) {
            for (int x : a) {
                cnt++;
                if (!x || x == 5)
                    ans += cnt;
            }
        } else
            ans = solve(0, n);
        cout << ans;
    }
    return 0;
}



Measured memory_bytes: 1680