#include using namespace std; using ll = long long; using P = pair; #define rep(i, n) for(ll i = 0; i < n; i++) #define all(x) (x).begin(),(x).end() templatebool chmin(T&a, const T&b){if(a>b){a=b;return 1;}return 0;} templatebool chmax(T&a, const T&b){if(a> n; vector a(n-1); rep(i,n-1){ cin >> a[i]; a[i]--; } vector dist(n,1001001001); dist[0] = 0; queue que; que.push(0); while(que.size()){ int now = que.front(); que.pop(); if(chmin(dist[a[now]], dist[now]+1)) que.push(a[now]); } if(dist[n-1]==1001001001) dist[n-1] = -1; cout << dist[n-1] << '\n'; return 0; }