import Data.List
main=do
 [n,p]<-map read.words<$>getLine
 s<-map(\c->fromEnum c-48).reverse<$>getLine
 let a=group$sort$0:(f 0 1 s p)
 print$sum$if 10`mod`p==0 then(map(\(c,i)->if c`mod`p==0 then i else 0)$zip(reverse s)[1..])else map(\l->(length l)*(-1+length l)`div`2)a
f w k[]p=[]
f w k(c:s)p=t:f t(k*10`mod`p)s p where t=(w+k*c)`mod`p