n,p=io.read("*n","*n","*l")
s=io.read()

combination=0
if p==2 or p==5 then
    for i=1,n do
        x=tonumber(s:sub(i,i))
        if x%p==0 then
            combination=combination+1
        end
    end
    print(combination)
    return
end

t={}
for i=1,p do
    t[i]=0
end
t[1]=1
digit=1
smod=0
for i=1,n do
    x=tonumber(s:sub(i,i))
    smod=(smod+x*digit)%p
    combination=combination+t[smod+1]
    t[smod+1]=t[smod+1]+1
    digit=(digit*10)%p
end

print(combination)