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