#include <iostream>

int main()
{
	int n;
	while (true) {
		std::cin >> n;
		if (n==0) break;
		int* array = new int[n];
		for (int i=0; i<n; i++) std::cin >> array[i];
		bool turn = true;
		int stones = 32;
		int index = 0;
		while (stones>0) {
			if (turn) stones -= (stones-1) % 5;
			else { stones -= array[index]; index = (index+1) % n; }
			if (stones<0) stones = 0;
			std::cout << stones << std::endl;
			turn = !turn;
		}
	}
	return 0;
}

