#
# Chain
#

while (n=gets.to_i)!=0

  queue=[]
  chain=[0]
  count=[0]

  n.times do
    char=gets.to_i
    if char==chain[-1]
      count[-1]+=1
    else
      chain<<char
      count<<1
    end
  end
  chain<<0
  count<<0

  # 色を変える候補
  2.upto(chain.size-2) do |i|
    if count[i]==1 && chain[i-1]==chain[i+1] && count[i-1]+count[i+1]>2
      queue<<[i, chain[i-1], -1]
    elsif count[i-1]>2
      queue<<[i, chain[i-1], -1]
    elsif count[i+1]>2
      queue<<[i, chain[i+1], 1]
    end
  end

  # 連鎖
  min=n
  while !queue.empty?

    _chain=chain.dup
    _count=count.dup

    idx, color, shift=queue.shift
    _chain[idx]=color

    if _count[idx]!=1
      if shift<0
        _chain.insert(idx, color)
        _count.insert(idx, 1)
      else
        _chain.insert(idx+1, color)
        _count.insert(idx+1, 1)
      end
    end

    idx+=shift

    while _chain[idx]==_chain[idx+1]
      _count[idx]+=_count[idx+1]
      _chain.delete_at(idx+1)
      _count.delete_at(idx+1)
      if _count[idx]>3
        _chain.delete_at(idx)
        _count.delete_at(idx)
      end
      if _chain[idx-1]==_chain[idx]
        idx-=1
      end
    end

    sum=0
    _count[1..-2].map { |c| sum+=c }
    min=sum if sum<min

  end

  puts min

end