N = int(input()) R = list(map(int, input().split())) dp = [float('inf')] * (N) dp[0] = 0 s = 0 g = R[s] while(1): dp[g-1] = min(dp[g-1], dp[s] + 1) if g == N: break s = g g = R[s] dp[N-1]