Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.  The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up. These buttons are numbered 1 through N. When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i. When Button i is not lighten up, nothing will happen by pressing it.  Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.  Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.
N=int(input())
a=[0]*N
for i in range(N):
  a[i]=int(input())

now=0
cnt=0
for j in range(N):
  #print(j,now,a[now]-1)
  if a[now]==2:
    print(cnt)
    break
  elif a[now]-1==now:
    print(-1)
    break
  else:
    now=a[now]-1
    cnt += 1
else:
  print(-1)
N=int(input())
a=[0]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())

now=1
cnt=0
for j in range(1,N+1):
  #print(j,cnt,now,a[now])
  if a[now]==2:
    cnt +=1
    print(cnt)
    break
  elif a[now]==now:
    print(-1)
    break
  else:
    now=a[now]
    cnt += 1
else:
  print(-1)