#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<double> CMP;

class TravelOnMars { public:
	int minTimes(vector <int> range, int startCity, int endCity)
	{
		int N = range.size();

		set<int> V;
		V.insert(startCity);
		for(int s=0 ;; ++s)
		{
			if(V.count(endCity))
				return s;
			set<int> VV;
			for(set<int>::iterator it=V.begin(); it!=V.end(); ++it)
			{
				int v = *it;
				for(int d=0; d<=range[v]; ++d) {
					VV.insert((v+d)%N);
					VV.insert((v-d+N*100)%N);
				}
			}
			V.swap(VV);
		}
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, TravelOnMars().minTimes(range, startCity, endCity));}
int main(){

CASE(0)
	int range_[] = {2,1,1,1,1,1};
	  vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 
	int startCity = 1; 
	int endCity = 4; 
	int _ = 2; 
END
CASE(1)
	int range_[] = {2,1,1,1,1,1};
	  vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 
	int startCity = 4; 
	int endCity = 1; 
	int _ = 3; 
END
CASE(2)
	int range_[] = {2,1,1,2,1,2,1,1};
	  vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 
	int startCity = 2; 
	int endCity = 6; 
	int _ = 3; 
END
CASE(3)
	int range_[] = {3,2,1,1,3,1,2,2,1,1,2,2,2,2,3};
	  vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 
	int startCity = 6; 
	int endCity = 13; 
	int _ = 4; 
END
CASE(4)
int range_[] = {10,1,1,1,4,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
	  vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 
	int startCity = 3; 
	int endCity = 10; 
	int _ = 3; 
END
CASE(5)
int range_[] = {50,50};
	  vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 
	int startCity = 0; 
	int endCity = 1; 
	int _ = 1; 
END
}
// END CUT HERE
