#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
using namespace std;

int main()
{
    int numOfStones;
    int *spots=new int[1000000];
    int *canGetThere=new int[1000000];
    int *stack=new int[1000000];
    while(scanf("%d",&numOfStones)!=EOF)
    {
        if(numOfStones==0)
        {
            return 0;
        }
        int stackCounter=0;
        memset(canGetThere,0,sizeof(int)*1000000);
        for(int i=0; i<numOfStones; i++)
        {
            scanf("%d",&spots[i]);
        }
        int max=0;
        for(int i=0; i<numOfStones; i++)
        {
            if(spots[0]+spots[i]==i)
            {
                canGetThere[i]=1;
                stack[stackCounter]=i;
                stackCounter++;
            }
        }
        for(int i=0; i<stackCounter; i++)
        {
            for(int k=0;k<numOfStones;k++)
            {
                if(canGetThere[k]!=1&&spots[stack[i]]+spots[k]==abs(k-stack[i]))
                {
                    stack[stackCounter]=k;
                    canGetThere[k]=1;
                    stackCounter++;
                }
            }
        }
        for(int i=numOfStones-1;i>=0;i--)
        {
            if(canGetThere[i]==1)
            {
                max=i;
                break;
            }
        }
        printf("%d\n",max);
    }
    delete []spots;
    delete []canGetThere;
    delete []stack;
    return 0;
}