import scala.annotation.tailrec

object Main extends App {

  import scala.io.StdIn._
  f

  def f: Unit = {
    val n = readLine.trim.toInt
    if (n == 0) return
    val num = toCh(List.tabulate(n){ _ ⇒ readLine.trim.toInt})
    println(n - search(num))
    f
  }
  def search(list: List[Ch]): Int = {
    def sub(prev: List[Ch], current: Ch, post: List[Ch], max: Int = 0): Int = {
      prev match {
        case c::t ⇒ sub(t, c, current::post, List(max, changeFirst(prev, current, post), changeLast(prev, current, post)).max)
        case _ ⇒ List(max, changeFirst(prev, current, post), changeLast(prev, current, post)).max
      }
    }
    list match {
      case Nil ⇒ 0
      case h::t ⇒ sub(t, h, Nil)
    }
  }
  def changeFirst(prev: List[Ch], current: Ch, post: List[Ch]): Int = {
    (prev, current, post) match {
      case (Ch(a, n)::t, Ch(_, 1), Ch(b, m)::u) if a == b && n + m >= 3 ⇒ n + m + 1 + remove(t, u)
      case (Ch(_, n)::t, Ch(a, m), _) if n >= 3 ⇒ if (m == 1) n + 1 + remove(t, post) else n + 1 + remove(t, Ch(a, m - 1)::post)
      case _ ⇒ 0
    }
  }
  def changeLast(prev: List[Ch], current: Ch, post: List[Ch]): Int = {
    (prev, current, post) match {
      case (Ch(a, n)::t, Ch(_, 1), Ch(b, m)::u) if a == b && n + m >= 3 ⇒ n + m + 1 + remove(t, u)
      case (_, Ch(a, n), Ch(_, m)::t) if m >= 3 ⇒ if (n == 1) m + 1 + remove(prev, t) else m + 1 + remove(Ch(a, n - 1)::prev, t)
      case _ ⇒ 0
    }
  }
  def remove(prev: List[Ch], post: List[Ch]): Int = {
    (prev, post) match {
      case (Ch(a, n)::t, Ch(b, m)::u) if a == b && n + m >= 4 ⇒ n + m + remove(t, u)
      case _ ⇒ 0
    }
  }
  @tailrec
  def toCh(list:List[Int], res: List[Ch] = Nil): List[Ch] = {
    list match {
      case Nil ⇒ res
      case h::t ⇒ res match {
        case Nil ⇒ toCh(t, Ch(h, 1)::res)
        case Ch(c, n)::rt if c == h ⇒ toCh(t, Ch(c, n + 1)::rt)
        case _ ⇒ toCh(t, Ch(h, 1)::res)
      }
    }
  }
  case class Ch(color: Int, num: Int)
}

