loop do
  n = gets.to_i
  break if n == 0
  go = [0]*(n+1)
  last_clr = gets.to_i
  go[0] = 1

  (2..n).each do |i|
    put = gets.to_i
    if put == last_clr
      j = go.index(0) - 1
      go[j] += 1
    else
      # put != last_clr
      if i.odd?
        j = go.index(0)
        go[j] = 1
      else
        # 連続する石の色を変える
        j = go.index(0) - 1
        if j > 0
          go[j-1] += go[j] + 1
          go[j] = 0
        else
          go[0] += 1
        end
      end
    end
    last_clr = put
  end

  # 石を数える
  cnt_w = 0
  last_i = go.index(0) - 1
  if last_clr == 0
    last_i.step(0, -2) do |i|
      cnt_w += go[i]
    end
  else
    (last_i - 1).step(0, -2) do |i|
      cnt_w += go[i]
    end
  end

  puts cnt_w
end