Scanf.scanf "%d" (fun n ->
    let p = Array.init n (fun _ -> Scanf.scanf " %d" (fun p -> p)) in

    let forward =
        let rec loop i cur =
            if i = n then n - cur + 1 else
                if p.(i) = cur then loop (i + 1) (cur + 1)
                               else loop (i + 1) cur
        in
        loop 0 1
    in
    let back =
        let rec loop i cur =
            if i < 0 then cur else
                if p.(i) = cur then loop (i - 1) (cur - 1)
                               else loop (i - 1) cur
        in
        loop (n - 1) n
    in
    Printf.printf "%d\n" @@ min forward back
)