Scanf.sscanf (read_line ()) "%d %d" (fun n p ->
    let s = read_line () in
    if p = 2 || p = 5 then (
        let rec loop i d acc =
            if i = n then acc else (
                let c = int_of_char s.[i] - int_of_char '0' in
                let d = if c mod p = 0 then d + 1 else d in
                let acc = acc + d in
                loop (i + 1) d acc
            )
        in
        loop 0 0 0 |> Printf.printf "%d\n"
    ) else (
        let dp = Array.make p 0 in
        dp.(0) <- 1;
        let rec loop i mul prev acc =
            if i < 0 then acc else
                let c = int_of_char s.[i] - int_of_char '0' in
                let d = (c * mul + prev) mod p in
                let () = dp.(d) <- dp.(d) + 1 in
                loop (i - 1) ((mul * 10) mod p) d (acc + dp.(d) - 1)
        in
        loop (n - 1) 1 0 0 |> Printf.printf "%d\n" 
    )
)