import sys input = sys.stdin.readline from collections import * N = int(input()) R = list(map(int, input().split())) G = [[] for _ in range(N)] for i in range(N-1): G[i].append((R[i]-1, 1)) for i in range(1, N): G[i].append((i-1, 0)) q = deque([0]) dist = [-1]*N dist[0] = 0 while q: v = q.popleft() for nv, w in G[v]: if dist[nv]!=-1: continue dist[nv] = dist[v]+w if w==0: q.appendleft(nv) else: q.append(nv) print(dist[-1])