def ichiro(n)
  (n-1)%5
end

def count(n)
  n < 0 ? 0 : n
end

while n = gets.to_i
  break if n.zero?

  jiro = gets.split.map(&:to_i)
  ohajiki = 32
  i = 0
  
  while 0 < ohajiki
    ohajiki -= ichiro(ohajiki)
    puts count(ohajiki)

    ohajiki -= jiro[i]
    puts count(ohajiki)

    if i == jiro.size-1
      i = 0
    else
      i += 1
    end
  end
end