import qualified Data.Vector.Unboxed as U
import qualified Data.ByteString.Char8 as C
import Data.List
import Data.Maybe
import qualified Data.IntMap as IM

r' :: IO [Int]
r'  =   unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine

main :: IO ()
main = do
    [n,p] <- r'

    let a +% b = (a + b) `rem` p
    let a *% b = (a * b) `rem` p
    let b ^% e = powAux (b `rem` p) e 1 `mod` p
            where
                powAux _ 0 ac = ac
                powAux b e ac = powAux (b*b `rem` p) (e `quot` 2)
                    (if odd e then b*ac `rem` p else ac)

    s <- getLine

    let fc = U.sum $ U.imap (\i d -> if read [d] `rem` p == 0 then i+1 else 0) $ U.fromList s
    let rems = scanr (+%) 0 $ map (`rem` fromIntegral p) $ zipWith (*%) [10 ^% x | x<-[n-1,n-2..0] ] $ map (read.pure) $ s

    if gcd 10 p == p then print fc
        else print $ sum $ snd $ mapAccumR (\mp a ->  (IM.alter (\x->case x of Nothing -> Just 1; Just y -> Just (y+1) ) a mp, fromMaybe 0 (IM.lookup a mp)) ) IM.empty rems
