#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int main(){
    int n;
    while(scanf("%d",&n), n){
        int v[100];
        rep(i,n)scanf("%d",v+i);
        int r=32;
        rep(i,1000){
            if(i&1){
                r -= min(r, v[(i/2)%n]);
            } else {
                r -= min(r, (r-1)%5);
            }
            printf("%d\n",r);
            if(r==0) break;
        }
    }
}