
open Scanf
open Printf

let flip f x y = f y x

let rec range a b = if a >= b then [] else a :: range (a+1) b

let read_int () = scanf " %d" (fun x -> x)
let rec read_ints n = if n = 0 then [] else let i = read_int () in i :: read_ints (n-1)

let rec for_iter a b f = if a >= b then [] else let x = f a in x :: for_iter (a+1) b f
let rec for_iter_ a b f = if a >= b then () else (f a; for_iter_ (a+1) b f)

let arrange xs' =
  let rec f c i xs =
    match xs with
    | [] -> c
    | [_] -> c
    | (x::y::ys) ->
        if x = i || y = i+1 then f (c+1) (i+1) (x::ys)
        else f c (i+1) (y::ys) in
  let xs, c = match xs' with
  | [] -> ([], 0)
  | (x::[]) -> ([x], 0)
  | (x::y::ys) -> if x = 1 then (x::ys, 1) else (y::ys, 0) in
  f c 2 xs

let () =
  let n = read_int () in
  read_ints n |> arrange |> printf "%d\n"

