import Data.Maybe
import Data.Char
import qualified Data.IntMap.Strict as M
import qualified Data.ByteString.Char8 as C

parseInt :: C.ByteString -> Int
parseInt = fst . fromJust . C.readInt

suffixSums :: Int -> C.ByteString -> M.IntMap Int
suffixSums p s = snd $ C.foldr' acc ((1, 0), M.fromList [(i, 0) | i <- [0 .. p-1]]) s
  where
  acc c ((x, y), mp) = ((xx, yy), M.adjust (\o -> o + 1) yy mp)
    where
    xx = 10 * x `mod` p
    yy = (x * digitToInt c + y) `mod` p

fpm :: Int -> Int -> Int -> Int
fpm p a e
  | e == 0 = 1
  | otherwise = (if odd e then a else 1) * aa * aa `mod` p
  where
  aa = fpm p a $ e `div` 2

solve :: Int -> C.ByteString -> Int
solve p s
  | n == 1 = if parseInt s `mod` p == 0 then 1 else 0
  | otherwise = cnt + solve p x + solve p y
  where
  n = C.length s
  (x, y) = C.splitAt (n `div` 2) s
  mp = suffixSums p x
  pp = fpm p 10 (p - 2)
  cnt = snd $ C.foldl' acc ((0, 0), 0) y
  acc ((val, len), total) c =
    let val' = (val * 10 + digitToInt c) `mod` p
        len' = len + 1
        o = ((-val') `mod` p + p) * (fpm p pp len') `mod` p
    in ((val', len'), total + (fromJust $ M.lookup o mp))

main :: IO ()
main = do
  [_, p] <- map parseInt . C.words <$> C.getLine
  s <- C.getLine
  putStrLn $ show $ solve p s