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

class Bod 
{
public:
  Bod (int d , int ind) 
  {
    value = d;
    flag = true;
    index = ind;
  }
  int value;
  int index;
  bool flag;
  void go (int& dist,vector<Bod>& ref )
  {
    
    flag = false;
    int body_size = body.size();
    if ( dist < index )
      { 
      dist = index;
      //cout << "Setting dist to :" << dist << endl;
      }
    for ( int i =0; i < body_size; i++)
      {
	//cout << " jump from " << index << " to " << body[i] << endl;
	if ( ref[body[i]].flag)
	  ref[body[i]].go(dist, ref);
      } 
      
  }
  vector<int> body;
};


int main ()
{
  ios_base::sync_with_stdio(false);
  int pocet;

  while ( cin >> pocet && pocet != 0)
    {
      int max_dist = 0;
      int d;
      vector <Bod>  body;
body.reserve(d);
      for ( int i =0; i < pocet ; i++)
	{
	  cin >> d; 
	  
	  body.push_back(Bod(d , i));
 body[i].body.reserve(d);
	  for ( int a = 0; a < i; a++)
	    {
	      // Bad aproximation 
	      if ( body[a].value + body[i].value == i - a )
		{
		  body[a].body.push_back(i);
		  body[i].body.push_back(a);
		}
	    }
	}
      body[0].go(max_dist , body);
      cout << max_dist << endl;
    }

  return 0;
}