#include<iostream>
using namespace std;

int main(){

  int n;

  while(cin >> n, n){
    int a[100],m = 32,j = 0,k = 0;

    for(int i=0;i<n;i++) cin >> a[i];

    while(1){
      if(j == n) j = 0;

      if(k == 0){
	m -= ((m-1)%5);
	k = 1;
      }

      else if(k == 1){
	m -= a[j];
	k = 0;
	j++;
      }
      if(m <= 0) break;
      cout << m << endl;
    }
    cout << "0" << endl;
  }
}