let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let p = Array.init n @@ fun _ -> int_of_string (read_line ())

let (l, l_cnt) = Array.fold_left (fun (a, cnt) p -> 
  if p = a then (a + 1, cnt)
  else if cnt = -1 then (p, 0)
  else (a, if p < a then cnt + 1 else cnt)
) (1, -1) p

let (r, r_cnt) = Array.fold_right (fun p (a, cnt) ->
  if p < l then (a, cnt)
  else if p = a then (a - 1, cnt)
  else if cnt = -1 then (p, 0)
  else (a, if a < p then cnt + 1 else cnt)
) p (n, -1)

let () = print_int ((if l_cnt = -1 then 0 else l - 1) + (if r_cnt = -1 then 0 else n - r))