import copy
count = 1
n = list(raw_input().split())
a = [int(i) for i in n]
b = list(copy.deepcopy(a[1:(len(n)//2)]))
minStep=[]
t =[]
# 把可能要走的路径保留下来
for x in b:
idx = a.index(x)
tem = [x]
y = x + idx
while True:
if y < len(n):
tem.append(a[y])
y = y + a[y]
else:
break
t.append(tem)
p,q =[],[]
# 捉一计算
for i in range(len(t)):
r =0
count = 1
for j in range(len(t[i])):
if count == 1:
idx = a.index(t[i][j])
r += idx
count += 1
r += t[i][j]
if r == len(n)-1:
p.append(count)
elif r > len(n):
q.append(-1)
break
if len(p)>0:
print(min(p))
else:
print(-1)