let () =
  Scanf.scanf "%d" @@ fun l ->
  let a = Array.init (l+1) @@ fun i ->
    if i = 0 then 0
    else Scanf.scanf " %d" ((+) 0)
  in
  for i = 1 to l do a.(i) <- a.(i) + a.(i-1) done;

  let dp1 = Array.make (l+1) 0 in
  let dp2 = Array.make (l+2) 0 in
  for i = 1 to l do
    dp1.(i) <- min a.(i) (dp1.(i-1) + (a.(i) - a.(i-1) + 1) mod 2)
  done;
  for i = l downto 1 do
    dp2.(i) <- min (a.(l) - a.(i-1)) @@
      dp2.(i+1) + if a.(i) = 0 then 2 else (a.(i) - a.(i-1)) mod 2
  done;

  Array.init (l+1) (fun i -> dp1.(i) + dp2.(i+1))
  |> Array.fold_left min max_int
  |> Printf.printf "%d\n"
