let rec power ( * ) e m n =
  if n <= 0 then e
  else power ( * ) (if n land 1 = 0 then e else m * e) (m * m) (n lsr 1)

let () = Scanf.scanf "%d %d\n%s" @@ fun n p s ->
  if p = 2 || p = 5 then begin
    Printf.printf "%d\n" @@
    Array.fold_left ( + ) 0 @@
    Array.init n @@ fun i ->
      if 0 < (Char.code s.[i] - Char.code '0') mod p
      then 0
      else i + 1
  end else begin
    let ( +^ ) x y = (x + y) mod p in
    let ( *^ ) x y = (x * y) mod p in
    let acc = Array.make (n + 1) 0 in
    for i = 0 to n - 1 do
      acc.(i + 1) <- acc.(i) +^ power ( *^ ) (Char.code s.[i] - Char.code '0') 10 ((p - 2) * (i + 1))
    done;
    let ms = Array.make p 0 in
    Array.iter (fun m -> ms.(m) <- 1 + ms.(m)) acc;
    Printf.printf "%d\n" @@
    Array.fold_right (fun m -> ( + ) @@ m * (m - 1) / 2) ms 0
  end