let _ = Scanf.scanf "%d\n" @@ fun n ->
  let ps = Array.init n @@ fun _ ->
    Scanf.scanf "%d\n" @@ fun p -> p in
  (* ps.(inv_ps.(i)) = i かつ inv_ps.(ps.(i)) = i なのを作る *)
  let inv_ps = Array.make (n + 1) 0 in
  Array.iteri (fun i p ->
    (* p = ps.(i) *)
    inv_ps.(p) <- i) ps;
  Array.init n (( + ) 1)
  |> Array.fold_left (fun (acc, s) p ->
      s :: acc,
      if inv_ps.(p - 1) <= inv_ps.(p)
      then s + 1
      else 1) ([], 0)
  |> (fun (acc, s) -> List.fold_left max s acc)
  |> (( - ) n)
  |> Printf.printf "%d\n"
