Scanf.sscanf (read_line ()) "%d %d" (fun n p ->
    let s = read_line () in
    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"
)