

#include <iostream>
#include <list>
using namespace std;

int main(int argc, const char * argv[]) {

    while (true) {
        int n;
        cin >> n;
        if(n == 0){
            break;
        }
        
        int suuretu[25] = {0};
        for(int i=0;i<n;i++){
            cin >> suuretu[i];
        }
        
        int sum = 32;
        bool ichiro = false;
        int i = 0;
        while(sum > 1){
            if(ichiro){
                sum -= suuretu[i];
                i = (i + 1) % n;
                ichiro = false;
            }
            else{
                sum -= (sum - 1) % 5;
                ichiro = true;
            }
            
            cout << sum << endl;
        }
        
        cout << 0 << endl;
    }
    
    
    
    return 0;
}