#include<iostream>
#define TARO 0
#define JIRO 1
using namespace std;

int main(){
	int i, str[25], marbles, flag, cou;
	
	while(1){
		cin >>i;
		if(i == 0) break;
		for(int j = 0; j < i; j++){
			cin >>str[j];
		}
		
		marbles = 32;
		cou = 0;
		flag = TARO;
		
		while(marbles){
			if(flag == TARO){
				marbles = marbles - ((marbles - 1) % 5);
				flag = JIRO;
				if(marbles < 0) marbles = 0;
			} else{
				marbles -= str[cou];
				cou++;
				if(cou >= i) cou = 0;
				flag = TARO;
				if(marbles < 0) marbles = 0;
			}
			cout <<marbles <<endl;
		}
	}
	return 0;
}