#include<iostream>
#include<vector>

int main()
{
	int n;
	while( std::cin >> n, n )
	{
		std::vector<int> v( n );
		for( int i = 0; i != n; ++i )
		{
			int a;
			std::cin >> a;
			v[i] = a;
		}

		int r = 32, p = 0;
		bool turn = false;
		while( true )
		{
			if( !turn )
				r -= ( r - 1 ) % 5;
			else
				r -= std::min( r, v[p] ), p = ( p + 1 ) % n;
	
			std::cout << r << std::endl;

			if( r == 1 )
				break;

			turn ^= true;
		}
	}

	return 0;
}