let rec fold_for a b v f = if a >= b then v else fold_for (a+1) b (f a v) f

let rec pow f z a n =
  if n = 0 then z
  else if n mod 2 = 1 then f a (pow f z a (n-1))
  else let h = pow f z a (n/2) in f h h

let rec intpow = pow ( * ) 1

let i3 = [| [|1; 0; 0|]; [|0; 1; 0|]; [|0; 0; 1|] |]

let () =
  Scanf.scanf "%d %d %d %d" @@ fun l a b modulo ->
  
  let (+@) a b = (a + b) mod modulo in
  let modmul a b = (a * b) mod modulo in
  let modpow = pow modmul 1 in

  let matmul a b =
    let m = Array.length b in
    Array.init (Array.length a) (fun i ->
    Array.init (Array.length b.(0)) (fun j ->
      fold_for 0 m 0 (fun k s -> s +@ modmul a.(i).(k) b.(k).(j))))
  in

  let rec loop i d z =
    if i >= l || d >= 19 then z else
    let j = (intpow 10 d - a - 1) / b |> min (l-1) in
    if a + b * j < intpow 10 (d-1) then loop i (d+1) z
    else ((
      let c = j-i+1 in
      let r = [| [| z; a +@ modmul b i; 1 |] |] in
      let m = [| [| modpow 10 d; 0; 0 |]; [| 1; 1; 0 |]; [| 0; b mod modulo; 1 |]; |] in
      let mc = pow matmul i3 m c in
      let r' = matmul r mc in
      loop (j+1) (d+1) r'.(0).(0)
    ))
  in
  loop 0 1 0 |> Printf.printf "%d\n"