#include <iostream>

using namespace std;

int main(){
	int count = 0;
	while(cin >> count){
		if(count == 0)
			break;

		int * pebbles = new int[count];
		char * visitedLeft = new char[count];
		char * visitedRight = new char[count];
		int * indexes = new int[count];		


		int max_distance = 0;
		int possible = 1;
		int possible_index = 0;		
		for(int i=0; i<count; i++){
			cin >> pebbles[i];	
			visitedLeft[i] = 0;
			visitedRight[i] = 0;		
			indexes[i] = 0;	
		}

		while(possible_index < possible && possible_index < count){
			int spots = pebbles[indexes[possible_index]];
			int another_index = indexes[possible_index] + spots;

			while(another_index >= 0 && another_index < count){
				if(pebbles[another_index] + spots == another_index - indexes[possible_index]){
					if(visitedLeft[another_index] != 0){
						break;
					} else {
						if(another_index > max_distance){
							max_distance = another_index;
						}
						if(visitedLeft[another_index] == 0 && visitedRight[another_index] == 0) {
							indexes[possible] = another_index;
							possible++;	
						}
						
						visitedLeft[another_index] = 1;
					}
				}
				another_index++;
			}

			another_index = indexes[possible_index] - spots;
			while(another_index >= 0 && another_index < count){
				if(pebbles[another_index] + spots == indexes[possible_index] - another_index ){
					if(visitedRight[another_index] != 0){
						break;
					} else {
						if( another_index  > max_distance){
							max_distance = another_index ;
						}
						if(visitedLeft[another_index] == 0 && visitedRight[another_index] == 0) {
							indexes[possible] = another_index;
							possible++;	
						}
						
						visitedRight[another_index] = 1;
					}
				}
				another_index--;
			}

			possible_index++;
		}

		cout << max_distance << endl;
	}
	return 0;
}