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
else
    t={}
    for i=1,p do
        t[i]=0
    end
    t[1]=1
    digit=1
    smod=0
    for i=n,1,-1 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
end
print(combination)