object Main extends App {
  import scala.io.StdIn._

  val l = readInt
  val ears = Array.tabulate(l){_ ⇒ readLong}
  println(solve(ears))
  def solve(ears: Array[Long]): Long = {
    val memo = Array.tabulate(3){_ ⇒ Array.tabulate(ears.length + 1){_ ⇒ None: Option[Long]}}//0, 0>E, 0>E>O
    memo(0)(0) = Some(0)
    memo(1)(0) = Some(0)
    memo(2)(0) = Some(0)
    def M(i: Int): Long = {
      memo(2)(i) match {
        case Some(r) ⇒ r
        case None ⇒
          List(M(i - 1) + (ears(i - 1) + 1) % 2, E(i - 1) + (if (ears(i - 1) == 0) 1 else 0), Z(i - 1)).min.also(r ⇒ memo(2)(i) = Some(r))
      }
    }
    def E(i: Int): Long = {
      memo(1)(i) match {
        case Some(r) ⇒ r
        case None ⇒
          List(E(i - 1) + (if (ears(i - 1) == 0) 2 else ears(i - 1) % 2), Z(i - 1) + ears(i - 1) % 2).min.also(r ⇒ memo(1)(i) = Some(r))
      }
    }
    def Z(i: Int): Long = {
      memo(0)(i) match {
        case Some(r) ⇒ r
        case None ⇒
          (Z(i - 1) + ears(i - 1)).also(r ⇒ memo(0)(i) = Some(r))
      }
    }
    val reversed = Array.tabulate(3){_ ⇒ Array.tabulate(ears.length + 1){_ ⇒ None: Option[Long]}}
    reversed(0)(0) = Some(0)
    reversed(1)(0) = Some(0)
    reversed(2)(0) = Some(0)
    def MR(i: Int): Long = {
      reversed(2)(i) match {
        case Some(r) ⇒ r
        case None ⇒
          List(MR(i - 1) + (ears(ears.length - i) + 1) % 2, ER(i - 1) + (if (ears(ears.length - i) == 0) 1 else 0), ZR(i - 1)).min.also(r ⇒ reversed(2)(i) = Some(r))
      }
    }
    def ER(i: Int): Long = {
      reversed(1)(i) match {
        case Some(r) ⇒ r
        case None ⇒
          List(ER(i - 1) + (if (ears(ears.length - i) == 0) 2 else ears(ears.length - i) % 2), ZR(i - 1) + ears(ears.length - i) % 2).min.also(r ⇒ reversed(1)(i) = Some(r))
      }
    }
    def ZR(i: Int): Long = {
      reversed(0)(i) match {
        case Some(r) ⇒ r
        case None ⇒
          (ZR(i - 1) + ears(ears.length - i)).also(r ⇒ reversed(0)(i) = Some(r))
      }
    }
    for (i ← 0 to ears.length by 3000) {
      M(i)
      MR(i)
    }
    (0 to ears.length).map(i ⇒ M(i) + MR(ears.length - i)).min
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline def also(@inline func: T ⇒ Unit): T = {
      func(value)
      value
    }
    @inline def let[A](@inline func: T ⇒ A): A = func(value)
  }
}