Problem:
Problem 3: 時空のスゴロク・ロード
全時空統一ディメンション・スゴロク・トーナメント。500 兆人を超える参加者の中から、ただ１人
のスゴロク絶対王者を決定するその大会に、あなたは２１世紀の地球代表として参加している。
今あなたが挑戦している課題は、自分自身をコマとした１次元スゴロク。端のスタートマスから出発
して、１から６までの目が一つずつ書かれた巨大な６面ダイスを振って出た目の数だけ進むことを繰
り返す、あなたもよく知っている形式のスゴロクだ。スタートマスとは反対の端にあるゴールマスに
止まるとゴールとなる。もちろん、ゴールするまでにサイコロを振った回数が少なければ少ないほど
良い成績となる。
マスの中には特殊な効果を持ったマス「○マス進む」と「○マス戻る」が存在し、そこに止まってし
まうと、指定されたマス数だけ進む、もしくは戻らなければならない。マスの効果で動いた結果、ふ
たたび効果のあるマスに止まった場合は、続けて指示どおりに移動する。
しかし、これは一筋縄では攻略できない時空スゴロクだ。恐るべきことに、たとえば「３マス進む」
の３マス先に「３マス戻る」が置かれていることもありうる。このようなマスに止まり、マスの効果
で無限ループに陥ってしまった場合は、永遠にマスを往復し続けなければならない。
だが幸いなことに、あなたの身体には、望んだ事象を全事象に変えることのできる異能『確率湾曲』
が宿っている。この能力を使えば、サイコロの出目を自由に操ることも可能。このアドバンテージを
活かして、無限ループに陥らないようにしながら進んでいくとき、ゴールするまでにサイコロを振る
回数の最小値はいくつになるだろうか。
Input
N
p
1
p
2
.
.
.
p
N
入力の１行目には、整数
N
（3 ≤
N
≤ 100,000）が書かれている。これは、スゴロクのマス数をあらわす。マスには1 番から
N
番までの番号がふられている。スタートのマスが1 番で、それからスタートに近い順に2 番、3 番、……、
N
- 1 番と続き、ゴールのマスが
N
番である。
i
番のマスでサイコロを振ってj の目が出たら、
i
+
j
番のマスへと移動する。ただし、もしも
i
+
j
が
N
を超えていたら、余った数だけ戻ったりはせず、ゴールしたとみなされる。
続く
N
行には、整数
p
i
（-100,000 ≤
p
i
≤ 100,000）が書かれている。1 ＋
i
行目に書かれた整数
p
i
は、i 番のマスに書かれている指示をあらわす。
p
i
> 0 ならば「
p
i
マス進む」、
p
i
< 0 ならば「-
p
i
マス戻る」であり、
p
i
= 0 ならばそのマスには何の効果もない。
p
1
と
p
N
は必ず0 である。マスの効果で、スタートより前やゴールより後に移動しろと指示されることはない。
なお、与えられるスゴロクは、ゴールすることが可能であると仮定してよい。
Output
ゴールするまでにサイコロを振る回数の最小値を出力せよ。
Sample Input 1
11
0
0
-2
0
-4
1
-1
2
0
0
0
Sample Output 1
3
Sample Input 2
12
0
0
7
0
2
0
0
3
-6
-2
1
0
Sample Output 2
1
Sample Input 3
8
0
4
-1
1
-2
-2
0
0
Sample Output 3
2

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    int N;
    cin >> N;
    auto f = [&](int p, int d) {
        int q = p+d;
        if (q < N) return q;
        else return N * 2 - 2 - q;
    };
    vector<int> A(N);
    rep(i,0,N) cin >> A[i];
    vector<int> D(N,inf);
    deque<int> Q;
    Q.push_front(0);
    D[0] = 0;
    while(!Q.empty()) {
        int P = Q.front();
        Q.pop_front();
        if (A[P] == 0) {
            rep(i,1,7) {
                int NP = f(P,i);
                if (chmin(D[NP],D[P]+1)) {
                    Q.push_back(NP);
                }
            }
        }
        else {
            int NP = f(P,A[P]);
            if (chmin(D[NP],D[P])) {
                Q.push_front(NP);
            }
        }
    }
    cout << D[N-1] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3856
final_rank: 2

code_snippet: #include <bits/stdc++.h>

int main() {
    int n;
    std::cin >> n;
    std::vector<int> next(n);
    for (int i = 0; i < n; i++) {
        int v;
        std::cin >> v;
        next[i] = i + v;
    }
    const int NONE = -2;
    const int LOOP = -1;
    std::vector<int> to(n, NONE);
    auto dfs = [&](auto self, int u) -> int {
        if (to[u] != NONE) {
            return to[u];
        } else {
            to[u] = LOOP;
            if (next[u] != u) {
                return to[u] = self(self, next[u]);
            } else {
                return to[u] = u;
            }
        }
    };
    for (int i = 0; i < n; i++) dfs(dfs, i);
    std::queue<int> que;
    std::vector<int> dist(n, NONE);
    que.push(0);
    dist[0] = 0;
    while (!que.empty()) {
        int u = que.front();
        assert(dist[u] != NONE);
        que.pop();
        for (int d = 1; d <= 6; d++) {
            int v = u + d;
            if (v >= n) continue;
            if (to[v] == LOOP) continue;
            if (dist[to[v]] == NONE) {
                dist[to[v]] = dist[u] + 1;
                que.push(to[v]);
            }
        }
    }
    std::cout << dist.back() << std::endl;
}
accuracy: 1
time_ms: 10
memory_kb: 9380
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
  std::cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> v(n);
  for(int i=0;i<n;i++){
    cin >> v[i];
  }
  // サイクルを検出
  vector<int> visited(n,-1);
  int num=0;
  map<int,bool> banned;
  for(int i=0;i<n;i++){
    if(visited[i]!=-1)continue;
    if(v[i]==0){
      visited[i]=num;
      banned.insert({num,true});
      num++;
      continue;
    }
    int next=i;
    while(v[next]!=0){
      if(visited[next]==num){
        banned.insert({num,false});
        break;
      }
      if(banned.count(visited[next])==1){
        if(banned[visited[next]]==false)banned.insert({num,false});
        break;
      }
      visited[next]=num;
      next+=v[next];
    }
    if(banned.count(num)==0)banned.insert({num,true});
    num++;
  }
  // bannned[visited[i]] = false ならサイクルなので踏み入れてはいけない
  vector<vector<pair<int,int>>> branch(n+1);
  for(int i=0;i<n;i++){
    // v[i]にコスト込みの辺を追加
    if(banned[visited[i]]==false)continue;
    if(v[i]!=0)
      branch[i].push_back({i+v[i],0});
    else{
      for(int j=1;j<=6;j++){
        if(i+j>=n+1)continue;
        branch[i].push_back({i+j,1});
      }
    }
  }
  // branchに辺情報を入れた
  vector<int> cost(n+1,-1);
  set<pair<int,int>> st;
  st.insert({0,0});
  while(!st.empty()){
    pair<int,int> p=*st.begin();
    st.erase(st.begin());
    if(cost[p.second]!=-1)continue;
    cost[p.second]=p.first;
    //cout << p.second << " " << cost[p.second] << endl;
    for(auto k:branch[p.second]){
      st.insert({cost[p.second]+k.second,k.first});
    }
  }
  if(banned[visited[n-1]]==false){
    cout << cost[n] << endl;
  }else{
    cout << cost[n-1] << endl;
  }
}
accuracy: 1
time_ms: 50
memory_kb: 14864
final_rank: 15

code_snippet: #include <bits/stdc++.h>

#define rep(i, N) for(ll i = 0; i < (ll)(N); ++i)
#define per(i, N) for(ll i = (ll)(N) - 1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bit(n, k) (((n) >> (k)) & 1)
#define pcnt(n) (bitset<64>(n).count())
#define endl '\n'
#define fi first
#define se second
#define mkpr make_pair
#define mktpl make_tuple
#define eb emplace_back
template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& V) {
    for (auto& it : V) is >> it;
    return is;
}
template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& V) {
    for (int i = 0 ; i < (int)V.size() ; i++) {
        os << V[i] << (i + 1 == (int)V.size() ? "" : " ");
    }
    return os;
}
using namespace std;

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int N; cin >> N;
    vector<int> A(N); cin >> A;

    vector<vector<int>> ableFrom(N);
    for(int i = 0; i < N; i++){
        if(A[i] != 0){
            int v = i + A[i];
            if(v < 0 || N <= v) continue;
            ableFrom[v].emplace_back(i);
        }
        else{
            for(int j = 1; j <= 6; j++){
                int v = i + j;
                if(v < 0 || N <= v) continue;
                ableFrom[v].emplace_back(i);
            }
        }
    }

    vector<int> dp(N, 1001001001); dp[N - 1] = 0;
    deque<int> deq;
    deq.push_back(N - 1);

    while(!deq.empty()){
        int u = deq.front();
        deq.pop_front();
        // cout << u << " " << dp[u] << endl;

        for(int& v : ableFrom[u]){
            int c = (A[v] == 0 ? 1 : 0);
            if(c == 0){
                if(dp[v] > dp[u]){
                    dp[v] = dp[u];
                    deq.push_front(v);
                }
            }
            else{
                if(dp[v] > dp[u] + 1){
                    dp[v] = dp[u] + 1;
                    deq.push_back(v);
                }
            }
        }
    }

    // cerr << dp << endl;

    cout << dp[0] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 9412
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;


int main() {

    int N;
    cin>>N;
    vector<int> P(N);
    for(auto &p:P)cin>>p;
    vector<int> C(N,-1);
    for(int i=0;i<N;i++){
        if(C[i]!=-1)continue;
        set<int> S;
        S.insert(i);
        int x=i;
        while(P[x]!=0){
            x+=P[x];
            if(S.count(x)){
                x=N+10;
                break;
            }
            S.insert(x);
        }
        for(auto s:S)C[s]=x;
    }
    priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> Q;
    Q.push({0,0});
    vector<int> D(N,1e9);
    vector<bool>seen (N,0);
    D[0]=0;
    while(!Q.empty()){
        int c=Q.top().first;
        int n=Q.top().second;
        Q.pop();
        if(seen[n])continue;
        seen[n]=1;
        for(int d=1;d<7;d++){
            int v=min(N-1,d+n);
            if(C[v]>N)continue;
            if(seen[C[v]])continue;
            if(D[C[v]]<c+1)continue;
            D[C[v]]=c+1;
            Q.push({c+1,C[v]});
        }
    }
    cout<<D[N-1]<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 7720
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;
queue<Pi> que;
int main(){
    int n, p[100001], masu[100001];
    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> p[i];
    }
    memset(masu, -1, sizeof(masu));
    que.push(Pi(0, 1));
    while(!que.empty()){
        Pi pi = que.front();
        que.pop();
        for(int i=1;i<=6;i++){
            int now = pi.second+i;
            if(now>n || masu[now]!=-1) continue;
            while(masu[now]==-1){
                masu[now] = pi.first+1;
                now += p[now];
            }
            if(p[now] == 0) que.push(Pi(pi.first+1, now));
        }
    }
    cout << masu[n] << endl;
    return(0);
}
accuracy: 1
time_ms: 10
memory_kb: 3824
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll n,p[200010],d[200010];
int main(void){
    cin>>n;
    for(ll i=0;i<n;i++){
        cin>>p[i];
        d[i]=(i==0 ? 0 : -1);
    }
    queue<ll> q;
    q.push(0);
    while(q.size()){
        ll num=q.front();
        q.pop();
        for(ll i=1;i<=6;i++){
            ll j=num+i;
            while(d[j]<0){
                d[j]=d[num]+1;
                if(p[j]){
                    j+=p[j];
                }else{
                    q.push(j);
                }
            }
        }
    }
    cout<<d[n-1]<<endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4928
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;
#define test cout << "test:"
const int inf = 1073741824;
const long long linf = 1152921504606846976;
// const int mod = 998244353;
// const int mod = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

void _main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];

	set<int> loop;
	for (int i = 0; i < n; i++)
	{
		if (loop.count(i))
			continue;

		if (a[i] == 0)
			continue;

		int now = i;
		vector<bool> vis(n, false);
		vis[now] = true;
		vector<int> history;
		history.push_back(i);	

		while (!vis[now] and now <= n - 1)
		{
			if (a[now] == 0)
				break;

			now += a[now];
			now = min(n - 1, now);
			now = max(0, now);

			if (now == 0 or now == n - 1)
				break;

			history.push_back(now);

			if (vis[now])
			{
				for (int i : history)
					loop.insert(i);

				break;
			}

			vis[now] = true;
		}
	}

	vector<int> ans(n, inf);
	queue<P> q;
	q.push({0, 0});

	while (!q.empty())
	{
		auto [now, cnt] = q.front();
		q.pop();
		// test;
		// cout << now << '\n';

		if (now >= n - 1)
		{
			ans[n - 1] = min(ans[n - 1], cnt);
			continue;
		}

		if (cnt >= ans[now])
			continue;

		else
			ans[now] = cnt;

		if (a[now] != 0)
		{
			if (!loop.count(now + a[now]))
			{
				int next = now + a[now];
				next = min(n - 1, next);
				next = max(0, next);
				q.push({next, cnt});
			}
		}

		else for (int i = 1; i <= 6; i++)
		{
			// test;
			if (loop.count(i + now))
				continue;

			else
				q.push({now + i, cnt + 1});
		}
	}

	cout << ans[n - 1] << '\n';
	return;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	_main();
}
accuracy: 1
time_ms: 4110
memory_kb: 4140
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;
#define test cout << "test:"
const int inf = 1073741824;
const long long linf = 1152921504606846976;
// const int mod = 998244353;
// const int mod = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

void _main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];

	set<int> loop;
	for (int i = 0; i < n; i++)
	{
		if (loop.count(i))
			continue;

		if (a[i] == 0)
			continue;

		int now = i;
		vector<bool> vis(n, false);
		vis[now] = true;
		vector<int> history;
		history.push_back(i);	

		while (!vis[now] and now <= n - 1)
		{
			if (a[now] == 0)
				break;

			now += a[now];
			now = min(n - 1, now);
			now = max(0, now);

			if (now == 0 or now == n - 1)
				break;
			history.push_back(now);

			if (vis[now])
			{
				for (int i : history)
					loop.insert(i);

				break;
			}
			
			vis[now] = true;

		}
	}

	vector<int> ans(n, inf);
	// dp[0] = 0;
	// for (int i = 0; i < n; i++)
	// {
	// 	if (loop.count(i))
	// 		continue;

	// 	if (a[i] < 0)
	// 	{
	// 		int cnt = 1;
	// 		int now = i + a[i];
	// 		while (!loop.count(now) and now > 0 and now <= i)
	// 		{
	// 			if (a[now] == 0)

	// 			now += a[now];
	// 			cnt++;
	// 		}

	// 		if (now > i)
	// 			dp[now] = min(dp[now], dp[i] + cnt);
	// 	}

	// 	else if (a[i] > 0)
	// 		dp[min(n - 1, i + a[i])] = min(dp[min(n - 1, i + a[i])], dp[i] + 1);

	// 	for (int j = 1; j <= 6; j++)
	// 	{
	// 		if (i + j >= n)
	// 			continue;


	// 		if (loop.count(i + j))
	// 			continue;

	// 		dp[i + j] = min(dp[i + j], dp[i] + 1);
	// 	}
	// }

	
	// test;
	// cout << dp[n - 1] << '\n';


	function<void(int, int)> dfs = [&](int now, int cnt)
	{
		if (now >= n - 1)
		{
			ans[n - 1] = min(ans[n - 1], cnt);
			return;
		}



		// cout << "serach:" << now << '\n';

		if (cnt >= ans[now])
			return;

		else
			ans[now] = cnt;

		if (a[now] != 0)
		{
			if (!loop.count(now + a[now]))
			{
				int next = now + a[now];

				next = min(n - 1, next);
				next = max(0, next);
				dfs(next, cnt);
			}
		}

		else for (int i = 1; i <= 6; i++)
		{
			if (loop.count(i + now))
				continue;

			else
				dfs(now + i, cnt + 1);
		}
	};
	dfs(0, 0);

	cout << ans[n - 1] << '\n';
	return;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	_main();
}
accuracy: 1
time_ms: 7810
memory_kb: 10352
final_rank: 19

code_snippet: //#include <atcoder/all>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll;
typedef vector<vector<long long> > vvll;
typedef vector<pair<ll,ll>> vpll;
typedef vector<string> vst;
typedef vector<bool> vbo;
typedef vector<vector<bool>> vvbo;
#define rep(i,a) for(int i=0;i<a;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define ALL(v) (v).begin(),(v).end()
#define RALL(v) (v).rbegin(),(v).rend()
ll gcd(ll a,ll b){return a?gcd(b%a,a):b;}
ll lcm(ll a,ll b){return a / gcd(a,b) * b;}
void oye(){cout << "Yes" << endl;}
void ono(){cout << "No"  << endl;}
//ll mod=998244353;
//ll mod=1e9+7;


int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n,now,d=1e9;cin>>n;
  vll p(n);
  vll dp(n,d);dp[0]=0;
  map<ll,ll> go;
  rep(i,n){
    go[i]=i;
  }
  vbo usable(n,true);
  rep(i,n){
    cin>>p[i];
  }
  REP(i,1,n-1){
    if(!usable[i] || go[i]!=i) continue;
    now=i;
    bool key=false;
    set<ll> wroop;
    wroop.insert(i);
    while(p[now]!=0){
      now+=p[now];
      if(!usable[now]){key=true;break;}
      if(wroop.count(now)){
        key=true;break;
      }
      wroop.insert(now);
    }
    if(key){
      for(ll j : wroop){
        usable[j]=false;
      }
    }
    else{
      for(ll j : wroop){
        go[j]=now;
      }
    }
  }
  queue<ll> nxt;
  nxt.push(0);
  vbo vst(n,false);vst[0]=true;
  while(dp[n-1]==d){
    now=nxt.front();nxt.pop();
    REP(i,1,7){
      if(now+i>=n) break;
      if(!usable[now+i] || vst[go[now+i]]) continue;
      nxt.push(go[now+i]);vst[go[now+i]]=true;
      dp[go[now+i]]=dp[now]+1;
    }
  }
  cout<<dp[n-1]<<endl;
}
accuracy: 1
time_ms: 80
memory_kb: 14960
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
const int INF = 1e9;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int N;
    cin >> N;
    vector<int> P(N);
    rep(i, N) cin >> P[i];

    vector<int> next(N, INF);
    rep(i, N) {
        if (next[i] != INF) continue;

        set<int> seen;
        auto rec = [&](auto &&self, int u) -> int {
            if (next[u] != INF) return next[u];
            if (P[u] == 0) return next[u] = u;
            if (seen.find(u) != seen.end())
                return next[u] = -1;

            seen.insert(u);
            return next[u] = self(self, u + P[u]);
        };
        rec(rec, i);
    }

    vector<int> d(N, INF);
    d[0] = 0;
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int u = que.front();
        que.pop();

        for (int i = 1; i <= 6; ++i) {
            if (u + i >= N) continue;

            int v = next[u + i];
            if (v == -1) continue;

            if (d[v] > d[u] + 1) {
                d[v] = d[u] + 1;
                que.push(v);
            }
        }
    }
    cout << d[N - 1] << "\n";

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 11652
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
const int INF = 1e9;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int N;
    cin >> N;
    vector<int> P(N);
    rep(i, N) cin >> P[i];

    vector<int> next(N, INF);
    rep(i, N) {
        if (next[i] != INF) continue;

        set<int> seen;
        auto rec = [&](auto &&self, int u) -> int {
            if (next[u] != INF) return next[u];
            if (P[u] == 0) return next[u] = u;
            if (seen.find(u) != seen.end())
                return next[u] = -1;

            seen.insert(u);
            return next[u] = self(self, u + P[u]);
        };
        rec(rec, i);
    }

    vector<int> d(N, INF);
    d[0] = 0;

    rep(i, N) {
        if (d[i] == INF) continue;

        for (int j = 1; j <= 6; ++j) {
            if (i + j >= N) break;
            int v = next[i + j];

            if (v == -1) continue;
            d[v] = min(d[v], d[i] + 1);
        }
    }
    cout << d[N - 1] << "\n";

    return 0;
}
accuracy: 0.55
time_ms: 20
memory_kb: 11644
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int>p(N);
    for(int i = 0; i < N; i++) {
        cin >> p[i];
    }
    vector<int>dist(N,-1);
    dist[0] = 0;
    vector<int>now = {0};
    for(int i = 0; i < N; i++) {
        queue<int>que;
        vector<int>nxt;
        for(int j = 0; j < now.size(); j++) {
            for(int k = 1; k <= 6; k++) {
                if(now[j]+k < N && dist[now[j]+k] == -1) {
                    dist[now[j]+k] = i+1;
                    if(p[now[j]+k] == 0) {
                        nxt.push_back(now[j]+k);
                    }
                    que.push(now[j]+k);
                }
            }
        }
        while (!que.empty()) {
            int x = que.front();
            que.pop();
            if(dist[x+p[x]] == -1) {
                dist[x+p[x]] = i+1;
                if(p[x+p[x]] == 0) {
                    nxt.push_back(x+p[x]);
                }
                que.push(x+p[x]);
            }
        }
        now = nxt;
        if(dist.back() != -1) {
            cout << dist.back() << endl;
            return 0;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3864
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

// g <- pair < v , cost > 
template < class T >
vector<T> Dijkstra(vector<vector<pair<int,T>>> &g, vector<int> starts) {
	const auto INF = numeric_limits<T>::max();
	vector<T> dist(g.size(), INF);
	priority_queue<pair<T,int>> Q;
	for(int s : starts) Q.emplace(0, s);
	while(!Q.empty()){
		T cost = Q.top().first;
		int v = Q.top().second;
		Q.pop();
		cost = - cost;
		if(dist[v] == INF){
			dist[v] = cost;
			for(auto e : g[v]){
				if(dist[e.first] == INF){
					Q.emplace(-(cost + e.second), e.first);
				}
			}
		}
	}
	return dist;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	
	int N; cin >> N;

	vector<vector<pair<int,int>>> G(N);
	rep(i,N) {
		int p; cin >> p;
		if(p == 0) {
			for(int k = 1; k <= 6; k++) {
				if(i + k < N)
					G[i].push_back({i + k, 1});
			}
		} else {
			G[i].push_back({i + p, 0});
		}
	}

	cout << Dijkstra(G, vector<int>{0}).back() << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 10596
final_rank: 13

code_snippet: #include <bits/stdc++.h>

using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

const int INF = 20202020;
int n;

/* dijkstra(G,s,dis)
    入力：グラフ G, 開始点 s, 距離を格納する dis
    計算量：O(|E|log|V|)
    副作用：dis が書き換えられる
*/
void dijkstra(int s, vector<int> &dis, vector<int> &cmd) {
    // 「仮の最短距離, 頂点」が小さい順に並ぶ
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    dis[s] = 0;
    pq.emplace(dis[s], s);
    while (!pq.empty()) {
        pair<int, int> p = pq.top();
        pq.pop();
        int v = p.second;
        // 最短距離で無ければ無視
        if (dis[v] < p.first) {
            continue;
        }

        if (cmd[v] != 0) {
            int nv = v + cmd[v];
            if (nv < 0 || n <= nv) continue;
            // 自己ループがやばい
            if (cmd[v] == -cmd[nv]) continue;
            // 最短距離候補なら priority_queue に追加
            if (dis[nv] > dis[v]) {
                dis[nv] = dis[v];
                pq.emplace(dis[nv], nv);
            }
            continue;
        }
        for (int i = 1; i <= 6; i++) {
            int nv = v + i;
            if (n <= nv) continue;
            // 最短距離候補なら priority_queue に追加
            if (dis[nv] > dis[v] + 1) {
                dis[nv] = dis[v] + 1;
                pq.emplace(dis[nv], nv);
            }
        }
    }
}

int main() {
    cin >> n;
    V<int> p(n);
    for (auto &&i : p) {
        cin >> i;
    }

    V<int> dis(n, INF);
    dijkstra(0, dis, p);
    // for (auto &&d : dis) {
    //     cout << d << " ";
    // }
    // cout << "\n";
    cout << dis[n - 1] << "\n";
}
accuracy: 1
time_ms: 10
memory_kb: 3872
final_rank: 4

code_snippet: #include <bits/stdc++.h>

using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

const int INF = 20202020;
int n;
/* dijkstra(G,s,dis)
    入力：グラフ G, 開始点 s, 距離を格納する dis
    計算量：O(|E|log|V|)
    副作用：dis が書き換えられる
*/
void dijkstra(int s, vector<int> &dis, vector<int> &cmd) {
    // 「仮の最短距離, 頂点」が小さい順に並ぶ
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    dis[s] = 0;
    pq.emplace(dis[s], s);
    while (!pq.empty()) {
        pair<int, int> p = pq.top();
        pq.pop();
        int v = p.second;
        // 最短距離で無ければ無視
        if (dis[v] < p.first) {
            continue;
        }

        if (cmd[v] != 0) {
            int nv = v + cmd[v];
            if (nv < 0 || n <= nv) continue;
            // 自己ループがやばい
            if (cmd[v] == -cmd[nv]) continue;
            // 最短距離候補なら priority_queue に追加
            if (dis[nv] > dis[v]) {
                dis[nv] = dis[v];
                pq.emplace(dis[nv], nv);
            }
            continue;
        }
        for (int i = 1; i <= 6; i++) {
            int nv = v + i;
            if (n <= nv) continue;
            if (nv + cmd[nv] < 0 || n <= nv + cmd[nv] || (cmd[nv] != 0 && cmd[nv] == -cmd[nv + cmd[nv]]))
                continue;
            // 最短距離候補なら priority_queue に追加
            if (dis[nv] > dis[v] + 1) {
                dis[nv] = dis[v] + 1;
                pq.emplace(dis[nv], nv);
            }
        }
    }
}

int main() {
    cin >> n;
    V<int> p(n);
    for (auto &&i : p) {
        cin >> i;
    }

    V<int> dis(n, INF);
    dijkstra(0, dis, p);
    // for (auto &&d : dis) {
    //     cout << d << " ";
    // }
    // cout << "\n";
    cout << dis[n - 1] << "\n";
}
accuracy: 1
time_ms: 10
memory_kb: 3896
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint2 = modint998244353;

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()
 
const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;
//const Int mod = 998244353;

typedef struct {
    int64_t to, weight;
} edge;

Int dx[] = {0, 1, 0, -1, -1, 1, -1, 1};
Int dy[] = {1, 0, -1, 0, -1, -1, 1, 1};

template<class T> 
istream &operator>>(istream &is, vector<T> &v) { 
    for (auto &e : v) {
        is >> e; 
    }
    return is; 
}

bool print_space_enable = false;
void print() { 
    std::cout << '\n'; 
    print_space_enable = false;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    if (print_space_enable) std::cout << " ";
    std::cout << fixed << setprecision(15) << head;
    print_space_enable = true;
    print(std::forward<Tail>(tail)...);
}

template<typename T>
void print(vector<T> v) {
    for (size_t i = 0; i < v.size(); i++) {
        if (i > 0) std::cout << " ";
        std::cout << v[i];
    }
    std::cout << '\n';
}

template<class T>
vector<T> make_vec(size_t n, T val) {
    return vector<T>(n, val);
}

template<class... Ts>
auto make_vec(size_t n, Ts... ts) {
    return vector<decltype(make_vec(ts...))>(n, make_vec(ts...));
}

bool is_overflow(Int a, Int b) {
    return a > INF / b;
}

// ダイクストラ
// vector<vector<edge>>, int64_t -> vector<int64_t>
vector<int64_t> dijkstra(vector<vector<edge>> &G, int64_t s) {
    int64_t node_num = G.size();
    vector<int64_t> dist(node_num, INF);
    priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>, greater<pair<int64_t, int64_t>>> q;
    q.push({0, s});
    dist[s] = 0;
    while (not q.empty()) {
        pair<int64_t, int64_t> p = q.top();
        q.pop();
        int64_t cur = p.second;
        if (dist[cur] < p.first) continue;
        for (int64_t i = 0; i < (int64_t)G[cur].size(); i++) {
            int64_t to = G[cur][i].to;
            int64_t cost = G[cur][i].weight;
            if (dist[to] > dist[cur] + cost) {
                dist[to] = dist[cur] + cost;
                q.push({dist[to], to});
            }
        }
    }
    return dist;
}

void solve() {
    Int n;
    cin >> n;
    vector<Int> a(n);
    cin >> a;
    auto G = make_vec(n * 2, 0, (edge){});
    auto in = make_vec(n, false);
    for (Int i = 0; i < n; i++) {
        for (Int j = 1; j <= 6; j++) {
            if (i + j < n) {
                G[i].push_back({n + i + j, 1});
            }
        }
        if (a[i] == 0) continue;
        G[n + i].push_back({n + i + a[i], 0});
        in[i] = true;
    }
    for (Int i = 0; i < n; i++) {
        if (in[i]) continue;
        G[n + i].push_back({i, 0});
    }
    auto d = dijkstra(G, 0);
    print(d[n - 1]);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    //Int _t; cin >> _t; while (_t--) solve();
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 27428
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

// clang-format off
//#include <atcoder/modint>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using vii = vector<int>;
using vll = vector<ll>;
using vdd = vector<ld>;
using vvii = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvdd = vector<vector<ld>>;
using vvvii = vector<vector<vector<int>>>;
using vvvll = vector<vector<vector<ll>>>;
using vvvdd = vector<vector<vector<ld>>>;
template<class T, class U> using P = pair<T, U>;
template<class T> using V1 = vector<T>;
template<class T> using V2 = vector<vector<T>>;
template<class T> using V3 = vector<vector<vector<T>>>;
template<class T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define _overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = 0; i < (n); i++)
#define rep3(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(...) _overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = (n) - 1; i >= 0; i--)
#define rrep2(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rrep3(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define rrep(...) _overload3(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define popcnt(x) __builtin_popcountll(x)
#define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end());
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const ld DINF = numeric_limits<ld>::infinity();
const ll mod = 1000000007;
//const ll mod = 998244353;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932;
//const ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
//const ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<typename T> T minV(const vector<T> &x) { return *min_element(all(x)); }
template<typename T> T maxV(const vector<T> &x) { return *max_element(all(x)); }
template<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.fi << ", " << p.se << ")"; }
template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "[ "; for (auto &z : v) os << z << " "; os << "]"; return os; }
#ifdef _DEBUG
#define show(x) cout << #x << " = " << x << endl;
#else
#define show(x)
#endif
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll rem(ll a, ll b) { return (a % b + b) % b; }
// clang-format on

// using mint = modint998244353;
// using mint = modint1000000007;

ll modpow(ll a, ll N, ll mod) {
    ll res = 1;
    // 例えば3=101(2)なので、下位bitから順に1ならa倍する
    while (N) {
        if (N & 1) res = res * a % mod;
        a = a * a % mod;
        N >>= 1;
    }
    return res;
}

int rec(vii &p, vii &memo, vii &seen, int i) {
    if (i >= sz(memo)) return sz(memo) - 1;
    // ↑「マスの効果で、スタートより前やゴールより後に移動しろと指示されることはない。」
    if (memo[i] != -1) return memo[i];
    if (seen[i] == 1) return memo[i] = inf;
    if (p[i] == 0) return i;
    seen[i] = 1;
    return memo[i] = rec(p, memo, seen, i + p[i]);
}

int main() {
    int N;
    cin >> N;
    vii p(N);
    rep(N) cin >> p[i];
    vii nx(N), memo(N, -1), seen(N, 0);
    rep(i, N) nx[i] = rec(p, memo, seen, i);
    vii dp(N, inf);
    dp[0] = 0;
    vii seen2(N, 0);
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int cur = que.front();
        que.pop();
        rep(j, 1, 7) {  // 出る目がj
            if (cur + j >= N) break;
            if (nx[cur + j] < inf) {    // 無限ループ
                if (chmin(dp[nx[cur + j]], dp[cur] + 1)) {
                    que.push(nx[cur + j]);
                }
            }
        }
    }
    cout << dp[N - 1] << endl;
    return 0;
}

/*
メモ
メモ化再帰で各マスに止まった時に最終的にどこに行くかを前計算する。
dp[0]=0としてdpする。
for i=0...n-1ではトポロジカル順序にはならないので、queueでBFSする
要領で更新する。
*/
accuracy: 1
time_ms: 10
memory_kb: 6740
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

// clang-format off
//#include <atcoder/modint>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using vii = vector<int>;
using vll = vector<ll>;
using vdd = vector<ld>;
using vvii = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvdd = vector<vector<ld>>;
using vvvii = vector<vector<vector<int>>>;
using vvvll = vector<vector<vector<ll>>>;
using vvvdd = vector<vector<vector<ld>>>;
template<class T, class U> using P = pair<T, U>;
template<class T> using V1 = vector<T>;
template<class T> using V2 = vector<vector<T>>;
template<class T> using V3 = vector<vector<vector<T>>>;
template<class T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define _overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = 0; i < (n); i++)
#define rep3(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(...) _overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = (n) - 1; i >= 0; i--)
#define rrep2(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rrep3(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define rrep(...) _overload3(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define popcnt(x) __builtin_popcountll(x)
#define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end());
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const ld DINF = numeric_limits<ld>::infinity();
const ll mod = 1000000007;
//const ll mod = 998244353;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932;
//const ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
//const ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<typename T> T minV(const vector<T> &x) { return *min_element(all(x)); }
template<typename T> T maxV(const vector<T> &x) { return *max_element(all(x)); }
template<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.fi << ", " << p.se << ")"; }
template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "[ "; for (auto &z : v) os << z << " "; os << "]"; return os; }
#ifdef _DEBUG
#define show(x) cout << #x << " = " << x << endl;
#else
#define show(x)
#endif
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll rem(ll a, ll b) { return (a % b + b) % b; }
// clang-format on

// using mint = modint998244353;
// using mint = modint1000000007;

ll modpow(ll a, ll N, ll mod) {
    ll res = 1;
    // 例えば3=101(2)なので、下位bitから順に1ならa倍する
    while (N) {
        if (N & 1) res = res * a % mod;
        a = a * a % mod;
        N >>= 1;
    }
    return res;
}

int rec(vii &p, vii &memo, vii &seen, int i) {
    if (memo[i] != -1) return memo[i];
    if (seen[i] == 1) return memo[i] = inf;
    if (p[i] == 0) return i;
    seen[i] = 1;
    return memo[i] = rec(p, memo, seen, i + p[i]);
}

int main() {
    int N;
    cin >> N;
    vii p(N);
    rep(N) cin >> p[i];
    vii nx(N, -1), memo(N, -1), seen(N, 0);
    rep(i, N) nx[i] = rec(p, memo, seen, i);
    vii dp(N, inf);
    dp[0] = 0;
    vii seen2(N, 0);
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int cur = que.front();
        que.pop();
        rep(j, 1, 7) {
            if (cur + j >= N) continue;
            if (nx[cur + j] < inf) {
                if (chmin(dp[nx[cur + j]], dp[cur] + 1)) {
                    que.push(nx[cur + j]);
                }
            }
        }
    }
    cout << dp[N - 1] << endl;
    return 0;
}

/*
メモ
メモ化再帰で各マスに止まった時に最終的にどこに行くかを前計算する

*/
accuracy: 1
time_ms: 10
memory_kb: 6632
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

// clang-format off
//#include <atcoder/modint>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using vii = vector<int>;
using vll = vector<ll>;
using vdd = vector<ld>;
using vvii = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvdd = vector<vector<ld>>;
using vvvii = vector<vector<vector<int>>>;
using vvvll = vector<vector<vector<ll>>>;
using vvvdd = vector<vector<vector<ld>>>;
template<class T, class U> using P = pair<T, U>;
template<class T> using V1 = vector<T>;
template<class T> using V2 = vector<vector<T>>;
template<class T> using V3 = vector<vector<vector<T>>>;
template<class T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define _overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = 0; i < (n); i++)
#define rep3(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(...) _overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = (n) - 1; i >= 0; i--)
#define rrep2(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rrep3(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define rrep(...) _overload3(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define popcnt(x) __builtin_popcountll(x)
#define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end());
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const ld DINF = numeric_limits<ld>::infinity();
const ll mod = 1000000007;
//const ll mod = 998244353;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932;
//const ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
//const ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<typename T> T minV(const vector<T> &x) { return *min_element(all(x)); }
template<typename T> T maxV(const vector<T> &x) { return *max_element(all(x)); }
template<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.fi << ", " << p.se << ")"; }
template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "[ "; for (auto &z : v) os << z << " "; os << "]"; return os; }
#ifdef _DEBUG
#define show(x) cout << #x << " = " << x << endl;
#else
#define show(x)
#endif
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll rem(ll a, ll b) { return (a % b + b) % b; }
// clang-format on

// using mint = modint998244353;
// using mint = modint1000000007;

ll modpow(ll a, ll N, ll mod) {
    ll res = 1;
    // 例えば3=101(2)なので、下位bitから順に1ならa倍する
    while (N) {
        if (N & 1) res = res * a % mod;
        a = a * a % mod;
        N >>= 1;
    }
    return res;
}

int rec(vii &p, vii &memo, vii &seen, int i) {
    if (i >= sz(memo)) return sz(memo) - 1;
    if (memo[i] != -1) return memo[i];
    if (seen[i] == 1) return memo[i] = inf;
    if (p[i] == 0) return i;
    seen[i] = 1;
    return memo[i] = rec(p, memo, seen, i + p[i]);
}

int main() {
    int N;
    cin >> N;
    vii p(N);
    rep(N) cin >> p[i];
    vii nx(N, -1), memo(N, -1), seen(N, 0);
    rep(i, N) nx[i] = rec(p, memo, seen, i);
    vii dp(N, inf);
    dp[0] = 0;
    vii seen2(N, 0);
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int cur = que.front();
        que.pop();
        rep(j, 1, 7) {
            if (cur + j >= N) continue;
            if (nx[cur + j] < inf) {
                if (chmin(dp[nx[cur + j]], dp[cur] + 1)) {
                    que.push(nx[cur + j]);
                }
            }
        }
    }
    cout << dp[N - 1] << endl;
    return 0;
}

/*
メモ
メモ化再帰で各マスに止まった時に最終的にどこに行くかを前計算する

*/
accuracy: 1
time_ms: 10
memory_kb: 6804
final_rank: 9