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.

-----Constraints-----
 - 2 ≤ N ≤ 10^5
 - 1 ≤ a_i ≤ N

-----Input-----
Input is given from Standard Input in the following format:
N
a_1
a_2
:
a_N

-----Output-----
Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

-----Sample Input-----
3
3
1
2

-----Sample Output-----
2

Press Button 1, then Button 3.

Difficulty: introductory

Starter code:

Solutions:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	sample
# CreatedDate:  2020-10-10 20:29:20 +0900
# LastModified: 2020-10-10 20:37:38 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    N = int(input())
    A = [0]
    visited = [0]*(N+1)
    for _ in range(N):
        A.append(int(input()))
    i = 1
    cnt = 0
    while visited[i] == 0:
        if i == 2:
            print(cnt)
            return
        cnt += 1
        visited[i] = 1
        i = A[i]
    print((-1))



def __starting_point():
    main()

__starting_point()

n = int(input())
a = [int(input())-1 for _ in range(n)]
curr_node = 0
for i in range(n):
    curr_node = a[curr_node]
    if curr_node == 1:
        print(i+1)
        return
print(-1)

N = int(input())
l = [int(input())-1 for _ in range(0,N)]

i = 0;
flg = False

for n in range(0,N):
  if l[i] == 1 :
    print(n+1)
    flg = True
    break
  else:
    i = l[i]


if not flg:
  print("-1")

N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
ans = -1
push = 0
count = [False]*N
 
for i in range(N+1):
 
    if A[push] == 1:
        ans = i+1
        break
        
    elif count[push]:
      break
      
    else:
        count[push] = True
        push = A[push]
        
      
print(ans)


N = int(input())
Buttons = []
for i in range(N):
  Buttons.append(int(input()))
pos = 0
count = 0
visit = [0]*N
flag = 1
while pos != 1:
  if visit[pos]!=0:
    flag = 0
    break;
  else:
    visit[pos]=1
    pos = Buttons[pos]-1
    count = count+1
if flag == 1:
  print(count)
else :
  print(-1)

n = int(input())
a = []
for i in range(n):
  a.append(int(input()))

res = -1
cc = 0
count = 0
for i in range(2*n):
  cc = a[cc] - 1
  count += 1
  if cc == 1:
    res = count
    break

print(res)

n = int(input())
a = [int(input())-1 for _ in range(n)]

visit_node = [0]*(n)
ans = 0
curr_node = 0

while(visit_node[curr_node] == 0):
    visit_node[curr_node] = 1
    curr_node = a[curr_node]
    ans += 1
    if curr_node == 1:
        print(ans)
        return
print(-1)

import sys
N = int(input())
A = [int(input()) for _ in range(N)]
x = 0
cnt = 0
for _ in range(N):
    x = A[x] - 1
    cnt += 1
    if x == 1:
        print(cnt)
        return
print(-1)

n = int(input())
light = [0] * (n+1)

for i in range(1,n+1):
  light[i] = int(input())
s = 1
ans = 0
while light[s] != 2 and ans <= n:
  s = light[s]
  ans += 1

if light[s] == 2:
  print(ans + 1)
else:
  print(-1)

#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc065/tasks/abc065_b

N = int(input())
a = [int(input())-1 for _ in range(N)]
ans = 0
cur = 0

visit = [-1] * N
while True:
    # print(cur, visit)
    cur = a[cur]
    ans += 1
    if visit[cur] == -1:
        visit[cur] = ans
    else:
        print(-1)
        return
    if cur == 1:
        print(ans)
        return

N=int(input())
a=[int(input())-1 for _ in range(N)]
x=0
ans=0
while True:
    ans+=1
    x=a[x]
    if x==1:
        print(ans)
        break
    if ans>N:
        print((-1))
        break;


import sys

n = int(input())
A = list(map(int, sys.stdin.readlines()))

index = 0
past_index = list()
found = False

for i in range(len(A)):
    if index == 1:
        print(i)
        found = True
        break
    index = A[index]-1

if not found:
    print(-1)

N = int(input())
A = [int(input()) for i in range(N)]
i = 0
cnt = 1
cmpr = [0]
while A[i] != 2:
    if A[i] == i + 1:
        print(-1)
        return
    i = A[i] - 1
    cmpr.append(i)
    cnt += 1
    if cnt > N:
        print(-1)
        return
print(cnt)

n=int(input())
a=[int(input()) for i in range(n)]
cnt=0
i=1
while i!=2:
    i=a[i-1]
    cnt+=1
    if cnt>10**5:
        cnt=-1
        break
print(cnt)


n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
total=1
i=a[0]-1
if a[0]==2:
    print(total)
else:
    while True:
        if a[i]==2:
            print(total+1)
            break
        elif total>n:
            print("-1")
            break
        else:
            i=a[i]-1
            total+=1

N = int(input())

lst = []
for i in range(N):
   lst.append(int(input()))

now = 1
count = 0

for i in range(N):
   now = lst[now - 1]
   count += 1
   if now == 2:
      print(count)
      return

print(-1)

from collections import defaultdict

n = int(input())
A = [int(input()) for _ in range(n)]

dic = defaultdict()

for i in range(n):
    dic[i+1] = A[i]

cnt, idx = 1, 0
val = dic[1]

while idx <= n:
    if val == 2:
        print(cnt)
        return
    val = dic[val]
    cnt += 1
    idx += 1
print((-1))


n=int(input())
a=[]
for _ in range(n):
    a.append(int(input()))
now=0
cnt=0
visit=[0 for _ in range(n)]
Flag=False
while now != 1:
    if visit[now] != 0:
        Flag=True
        break
    else:
        visit[now]=1
        now=a[now]-1
        cnt+=1
if Flag:
    print('-1')
else:
    print(cnt)


n = int(input())
q = [int(input())  for i in range(n)]
cnt = 0
ind = 0
for i in range(n):
    ind = q[ind]-1
    cnt += 1
    if ind == 1:
        print(cnt)
        return
    
print((-1))



n = int(input())
a = []
b = []
for i in range(n):
    a.append(int(input()))

next = a[0] - 1
b.append(next + 1)
for i in range(n):
    next = a[next] - 1
    b.append(next + 1)
print(b.index(2) + 1) if 2 in b else print(-1)

# -*- coding:utf-8 -*-

import sys
nyuryoku = [int(i) - 1 for i in sys.stdin]

N = nyuryoku[0] + 1
a_list = nyuryoku[1:]


cnt_push = 0
button_list = [False] * N
i = 0
#cur_but_posi = a_list[0]

while True:
    if button_list[i] == False:
        button_list[i] = True
        cnt_push += 1
        if a_list[i] == 1:
            break
        i = a_list[i]
    else:
        cnt_push = -1
        break

print(cnt_push)

"""
import sys

l = [int(i) - 1 for i in sys.stdin]

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)
"""


n=int(input())
a=[int(input()) for _ in range(n)]
light=0
ans=0
for i in range(n):
    if a[light]==2:
        ans+=1
        break
    else:
        light=a[light]-1
        ans+=1
else:
    ans=-1
print(ans)

N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
ans = -1
push = 0
count = set()
 
for i in range(N+1):
 
    if A[push] == 1:
        ans = i+1
        break
        
    elif A[push] in count:
      break
      
    else:
      count.add(push)
      push = A[push]
        
print(ans)


n = int(input())
done = [False for _ in range(n)]
A = [int(input()) - 1 for _ in range(n)]
tmp = 0
for i in range(n + 1):
    if A[tmp] == 1:
        print((i + 1))

        break
    if done[tmp]:
        print((-1))
        break
    else:
        done[tmp] = True
    tmp = A[tmp]
else:
    print((-1))


N = int(input())
A = [int(input()) for _ in range(N)]
A = [a-1 for a in A]

fin = set()
cnt = 0
i = 0
while True:
    if i in fin:
        cnt = -1
        break
    if i == 1:
        break
    cnt += 1
    fin.add(i)
    i = A[i]
print(cnt)

# B - Trained?
def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    cnt = 0
    x = 1

    if 2 in a:
        for _ in range(n-1):
            x = a[x-1]
            cnt += 1

            if x == 2:
                print(cnt)
                return
        else:
            print(-1)
    else:
        print(-1)
        

if __name__ ==  "__main__":
    main()

N = int(input())
A = [0]*(N+1)
for i in range(1,N+1):
    A[i] = int(input())

is_visited_list = [0]*(N+1)

loop = False
now_visit = 1
cnt = 0
while True:
    if is_visited_list[now_visit] == 1:
        loop = True
        break
    else:
        is_visited_list[now_visit] = 1
    
    now_visit = A[now_visit]
    if now_visit == 2:
        cnt +=1
        break
    else:
        cnt +=1

if loop:
    print(-1)
else:
    print(cnt)

N = int(input())
A = [0] * N
for i in range(N):
    A[i] = int(input()) - 1 # 配列に対応
index = 0
trials = -1
for i in range(N):
    index = A[index]
    if index == 1:
        trials = i + 1
        break
print(trials)


N = int(input())
a = [int(input()) for _ in range(N)]
cnt = 0
on = 0
for _ in range(N):
    cnt += 1
    if a[on] == 2:
        print(cnt)
        return
    on = a[on]-1
print(-1)

N = int(input())
a = [int(input()) for i in range(N)]

count = [0]*(N)

i = 1
min_num = 0
count[i-1]+=1
while True:
    i = a[i-1]
    min_num += 1
    if i == 2:
        print(min_num)
        return
    if count[i - 1] == 0:
        count[i-1]+=1
    else:
        break

print(-1)

n = int(input())
q = [int(input())  for i in range(n)]
cnt = 0
ind = 0
for i in range(n):
    ind = q[ind]-1
    cnt += 1
    if ind == 1:
        print(cnt)
        return
    
print(-1)

import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

N = k()
A = []
for i in range(N):
    a = k()
    A.append(a)
    
aaa = 0    
ans = A[0]
count = 1
 
for i in range(N):
    if ans == 2:
        aaa = 1
        break
    else:
        ans = A[ans-1]
        count += 1
if aaa == 1:
    print(count)
else:
    print(-1)

# N<=10^5なので愚直にやっても間に合う
n = int(input())
a = [int(input()) - 1 for _ in range(n)]
visited = [False] * n
now = 0
count = 0
while visited[now] != True:
    count += 1
    visited[now] = True
    now = a[now]
    if now == 1:
        print(count)
        return
print((-1))


N = int(input())
a = []
 
for _ in range(N):
    a.append(int(input()) - 1)

i, count = 0, 0
success = False
for _ in range(N):
    i = a[i]
    count += 1

    if i == 1:
        success = True
        break
 
if not success:
    count = -1
 
print(count)

N = int(input())
A = [int(input()) for _ in range(N)]

A = [0] + A
i = 1
k = 1
while k <= N:
    if A[i] == 2:
        print(k)
        return
    i = A[i]
    k += 1
print((-1))


N = int(input())
num_list = [int(input()) for _ in range(N)]
num = 0
for i in range(N):
    if num_list[num] == 2:
        print(i+1)
        return
    num = num_list[num]-1
print(-1)

N=int(input())
a=[None]+[int(input()) for i in range(N)]
c=1

for i in range(1, N+1):
  if a[c]==2:
    print(i)
    return
  else:
    c=a[c]+0
    
print(-1)

n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())


ans = -1
memo = 0
light = 1

for i in range(n):
    if light == 2:
        ans = memo
        break
    light = a[light-1]
    memo += 1


print(ans)

n = int(input())
a = [int(input()) for i in range(n)]
cnt = 0

if not a.count(2):
    print('-1')
else:
    i = 0
    while True:
        cnt += 1
        if a[i] == 2:
            print(cnt)
            break
        elif a[i] == i+1 or cnt > n:
            print('-1')
            break
        i = a[i]-1

import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    a = [I() - 1 for _ in range(N)]

    c = 0
    ans = 0
    is_ok = True
    while True:
        c = a[c]
        ans += 1
        if c == 0 or ans > N:
            is_ok = False
            break
        if c == 1:
            break

    if is_ok:
        print(ans)
    else:
        print(-1)

def __starting_point():
    resolve()
__starting_point()

#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc065/tasks/abc065_b

N = int(input())
a = [int(input())-1 for _ in range(N)]
ans = 0
cur = 0

visit = [-1] * N
while True:
    # print(cur, visit)
    cur = a[cur]
    ans += 1
    if visit[cur] == -1:
        visit[cur] = ans
    else:
        print((-1))
        return
    if cur == 1:
        print(ans)
        return


n = int(input())
a = [0]
for i in range(n):
    a.append(int(input()))
cnt = 0
a_before = 1
while cnt <= n:
    cnt += 1
    if a[a_before] == 2:
        print(cnt)
        break
    a_before = a[a_before]
else:
    print(-1)

n = int(input())
li = [0]
for i in range(n):
    li.append(int(input()))

count = 0
now = 1
while now != 2:
    if li[now] == 0:
        count = -1
        break
    else:
        a = li[now]
        li[now] = 0
        now = a
        count += 1

print(count)

n=int(input())
a=[int(input())-1 for _ in range(n)]
id=[-1]*n
x=0
for c in range(1,n+1):
    if id[x]!=-1:
        print(-1)
        return
    id[x]=c
    x=a[x]
    if x==1:
        print(c)
        return

n = int(input())

a = [int(input()) for _ in range(n)]
a.insert(0,0)

pos = 1
for i in range(1,n+1):
  pos = a[pos]
  if pos==2:
    print(i)
    break
if pos!=2:
  print((-1))


n = int(input())
a = list(map(int, [input() for i in range(n)]))
ans = 1
hikari = 1
for i in range(n):
        hikari = a[hikari -1]
        if hikari == 2:
                print(ans)
                return
        else:
                ans  += 1
print(-1)

n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
    
a.insert(0, 0)
ans = -1
push = 1

for i in range(1, n+2):
    
    if a[push] == 2:
        ans = i
        break
        
    else:
        push = a[push]
      
print(ans)

n = int(input())
al = [int(input()) for i in range(n)]

i = 0
for j in range(n):
    i = al[i]-1
    if i == 1:
        print(j+1)
        return
print(-1)

n = int(input())
#x, a, b = map(int, input().split())
#l = list(map(int,input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
al = []
for i in range(n):
    a = int(input())
    al.append(a)

al.insert(0, 0)
ans = -1
push = 1
for i in range(1, n+2):
    light = al[push]
    push = light
    if light == 2:
        ans = i
        break
print(ans)


n=int(input())
a=[int(input()) for i in range(n)]
ans=1
res=1
for i in range(n):
  if a[res-1]==2:
    print(ans)
    return
  else:
    ans+=1
    res=a[res-1]
print(-1)


def resolve():
    N = int(input())
    A = [ int(input()) for _ in range(N)]

    #print(N, A)

    next = 0
    cnt = 0
    for i in range(N):
        next = A[next] - 1
        #print(next)
        cnt += 1
        if next == 1:
            break

    if cnt == N:
        print(-1)
    else:
        print(cnt)


def __starting_point():
    resolve()
__starting_point()

N = int(input())
A = [0] + [int(input()) for _ in range(N)]

j = 1
cnt = 0
check = False

for i in range(N):
  if j == 2:
    check= True
    break
  cnt += 1
  j = A[j]
  
print(cnt if check else -1)

# -*- coding: utf-8 -*-

N = int(input())
a = []
for i in range(N):
    a.append(int(input()))

ans = pos = 0
flag = [0] * N
while True:
    ans += 1
    if a[pos] == 2:
        break
    #print(pos,a[pos]-1)
    pos = a[pos] - 1

    if flag[pos] > 0:
        ans = -1
        break
    else:
        flag[pos] = 1

print(ans)

n = int(input())
l = []
for i in range(n):
    a = int(input())
    l.append(a)
x = l[0]
cnt = 1
for i in range(n):
    if x == 2:
        print(cnt)
        return
    else:
        cnt +=1
        x = l[x-1]
print('-1')

n = int(input())
A = []
bool = [True] * n
for i in range(n):
    a = int(input())
    a -= 1
    A.append(a)
j = A[0]
cnt = 1
for i in range(n):
    if j == 1:
        print(cnt)
        return
    if bool[A[j]] == True:
        bool[A[j]] = False        
        j = A[j]
        cnt += 1
    else :
        print((-1))
        return




n = int(input())
a = [int(input()) for _ in range(n)]
# a = list(map(lambda x: int(x) - 1, a))	#すべての要素から-1

p = a[0]

if p == 2:
    print(1)
    return
    
cnt = 1

for _ in range(n):
    p = a[p-1]
    cnt += 1
    if p == 2:
        print(cnt)
        return

print(-1)

import re
import copy

def accept_input():
    N = int(input())
    A = {}
    for i in range(1,N+1):
        A[i] = int(input())
    return N,A

def process(s):
    if s == "#":
        return 1
    else:
        return 0
N,A = accept_input()
cur = 1
kaisu = 0
for _ in range(N):
    cur = A[cur]
    kaisu += 1
    if cur == 2:
        break
if cur == 2:
    print(kaisu)
else:
    print((-1))


N = int(input())
d = []
visited = [False for _ in range(N+1)]
for _ in range(N):
  d.append(int(input())-1)

i = 0
cnt = 0
while True:
  if i == 1:
    break
  elif cnt >= N:
    cnt = -1
    break
  elif visited[i]:
    cnt = -1
    break
  else:
    visited[i] = True
    cnt += 1
    i = d[i]
  
print(cnt)

n = int(input())
a = [0] * n
for i in range(n):
    a[i] = int(input())

visit = set()
now = 1
for i in range(1, n + 10):
    if now not in visit:
        visit.add(now)
        now = a[now - 1]
    else:
        print((-1))
        break
    if now == 2:
        print(i)
        break


n = int(input())
line = [0]
for i in range(n):
  line.append(int(input()))
check = [0 for i in range(n+1)]
count = 0
check[1] = 1
a = 1
while True:
  count += 1
  if line[a] == 2:
    break
  else:
    a = line[a]
    check[a] += 1
  
  if check[a] > 1:
    count = -1
    break
print(count)
  


with open(0) as f:
    N, *a = map(int, f.read().split())

from itertools import count
p = 0
seen = {0}
for i in count(1,1):
    p = a[p]-1
    if p == 1:
        ans = i
        break
    if p in seen:
        ans = -1
        break
    seen.add(p)
print(ans)

N = int(input())
a = [0,]
for i in range(N):
    a.append(int(input()))
idx = 1
for i in range(1,N+1):
    idx = a[idx]
    if idx == 2:
        print(i)
        return
print(-1)

N = int(input())
a = [int(input()) for _ in range(N)]
log = set()
i = 0
count = 1
while a[i] != 2:
  i = a[i] - 1
  if i in log:
    print(-1)
    break
  else:
    log.add(i)
    count += 1
else:
  print(count)

#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect
from fractions  import gcd

n=int(input())
p=set()
p.add(0)
ai=[int(input()) for _ in range(n)]
s=0
cnt=0
while True:
    a=ai[s]
    if a==2:
        print(cnt+1)
        return
    if a-1 not in p:
        p.add(a-1)
        cnt+=1
        s=a-1
    else:
        print(-1)
        return

N = int(input())

l = []
for i in range(N):
  v = int(input())
  l.append(v)
  
v = l[0]
ans = 1
while v != 2 and v != -1:
  tmp = l[v-1]
  l[v-1] = -1
  v = tmp
  ans += 1
  
if v == 2:
  print(ans)
else: 
  print(-1)

import sys
N = int(input())
A ={}
for i in range(1,N+1):
    A[i] = int(input())
#print(A)
ans = 1
x = 1
for j in range(N):
    if A[x] == 2:
        print(ans)
        return
    x = A[x]
    ans += 1
print(-1)

N = int(input())
A = []
for i in range(N):
    a = int(input())
    A.append(a)
b = 1
x = A[0]
while x != 2:
    x = A[x - 1]
    if b > N:
        print(-1)
        return
    b += 1

print(b)

n = int(input())
al = []
for i in range(n):
    a = int(input())
    al.append(a)
 
al.insert(0, 0)
ans = -1
push = 1
for i in range(1, n+2):
    light = al[push]
    push = light
    if light == 2:
        ans = i
        break
print(ans)

def main():
    N = int(input())
    a = [int(input()) for _ in range(N)]
    cnt = 0
    tmp = 1
    while cnt<=100001:
        tmp = a[tmp-1]
        cnt += 1
        if tmp == 2:
            return cnt
        elif tmp == 1:
            return -1

    return -1

print((main()))


n=int(input())
a=[int(input()) for i in range(n)]
ans=1
res=1
for i in range(n):
  if a[res-1]==2:
    print(ans)
    return
  else:
    ans+=1
    res=a[res-1]

print(-1)

N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
#A.insert(0, 0)
ans = -1
push = 0

for i in range(N+1):

    if A[push] == 1:
        ans = i+1
        break
        
    else:
        push = A[push]
      
print(ans)


from typing import List


def answer(n: int, a: List[int]) -> int:
    next_value = 1
    count = 0
    for _ in range(n):
        next_value = a[next_value - 1]
        count += 1
        if next_value == 2:
            return count

    return -1


def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    print((answer(n, a)))


def __starting_point():
    main()

__starting_point()

n = int(input())
buttons = [int(input()) for _ in range(n)]
done = [0] * (n + 1)
next_button = buttons[0]
for _ in range(n):
    buttons_status = done[next_button]
    if buttons_status != 0:
        print((-1))
        return
    done[next_button] = 1
    if next_button == 2:
        break
    next_button = buttons[next_button - 1]
print((done.count(1)))


LI = lambda: list(map(int, input().split()))

N = int(input())
A = [int(input()) for _ in range(N)]


def main():
    a = 1
    for i in range(N + 1):
        if a == 2:
            print(i)
            return 0
        a = A[a - 1]
    print((-1))


def __starting_point():
    main()

__starting_point()

n = int(input())
a = []
s = [0]*n
x = 1
for i in range(n):
  a.append(int(input()))
if 2 not in a:
  print(-1)
else:
  while True:
    if a[x-1] == 2:
      print(sum(s)+1)
      return
    else:
      s[x-1] += 1
      if s[x-1] == 2:
        print(-1)
        return
      x = a[x-1]

n = int(input())
btn = [0]
for i in range(n):
  btn.append(int(input()))
see = [0 for i in range(n+1)]
  
now = 1
cnt = 0

see[now] = 1
while True:
  now = btn[now]
  cnt += 1
  
  if now == 2:
    print(cnt)
    break
  
  if see[now] == 1:
    print((-1))
    break
  
  see[now] = 1


n = int(input())
a = [0] + [int(input()) for i in range(n)]
cnt = 1
next_a = a[1]

if a[1] == 2:
    print(cnt)

else:
    while cnt <= 10**5:
        next_a = a[next_a]
        cnt += 1
        if next_a == 2:
            break

    if cnt <= 10**5:
        print(cnt)
    else:
        print(-1)

n = int(input())
A = [int(input()) for _ in range(n)]

now = 0
cnt = 0
while A[now] != 2 and cnt <= n:
    now = A[now] - 1
    cnt += 1

if A[now] == 2:
    print((cnt + 1))
else:
    print((-1))


def f():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    x = 1
    c = 0
    for i in range(n):
        if x != 2:
            x = a[x-1]
            c += 1
            if c == n:
                return -1
        else:
            return c
print(f())

import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: list(map(int, sys.stdin.readline().split()))
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
a = [ri() for _ in range(n)]
idx = 1
cnt = 1
if a[0] == 2:
    print((1))
    return
while idx != -1:
    a[idx-1], idx = -1, a[idx-1]
    cnt += 1
    if a[idx-1] == 2:
        print(cnt)
        return
print((-1))












import sys
N = int(input())
a = [int(input()) for _ in range(N)]
flag = [0]*N
k = 0
i = 0
flag[0] = 1
while k <N:
  k += 1
  i = a[i] - 1
  if flag[i] == 0:
    flag[i] = 1
  elif flag[i] == 1:
    print(-1)
    return
  if i == 1:
    print(k)
    return

import sys
N = int(input())
d = [int(input()) for i in range(N)]

cnt = 0
button = d[0]
while button != 2:
    button = d[button-1]
    cnt += 1

    if cnt > 100000:
        print(-1)
        return

print(cnt+1)

N = int(input())
L = []
light = [0]*(N)
light[0] = 1
cnt, now = 0, 0

for i in range(N):
    L.append(int(input()))

for i in range(N):
    now = L[now] - 1
    cnt += 1
    if now == 1:
        print(cnt)
        return

print((-1))


N = int(input())
V = [int(input()) for i in range(1,N+1)]
cnt = 1
val = V[0]
while True:
  if val == 2:
    print(cnt)
    break
  if (cnt >= N):
    print((-1))
    break
  cnt += 1
  val = V[val-1]


n = int(input())
al = list(int(input()) for _ in range(n))
num = [0]*n
i = 0
cnt = 0
while True:
    if al[i] == 2:
        print((cnt+1))
        break
    else:
        if num[al[i]-1] == 1:
            print((-1))
            break
        else:
            num[al[i]-1] += 1
            i = al[i]-1
            cnt += 1


n = int(input())
A = [int(input()) for _ in range(n)]

now = 0
cnt = 0
while A[now] != 2 and cnt <= n:
    now = A[now] - 1
    cnt += 1

if A[now] == 2:
    print((cnt + 1))
else:
    print((-1))


N=int(input())
a=[]
for i in range(N):
  a.append(int(input())-1)
flag=True
step=[]
pivot=0
for i in range(N+1):
  step.append(pivot)
  if a[pivot]==1:
    break
  pivot=a[pivot]

if len(step)>N:
  print(-1)
else:
  print(len(step))

N = int(input())
A = [int(input())-1 for _ in range(N)]
cnt = 1
next_button = 0
while cnt <= len(A):
    next_button = A[next_button]
    if next_button == 1:
        print(cnt)
        return
    cnt += 1
print((-1))


n = int(input())
l = [0 for _ in range(n)]
already = set()
next = 1
c = 0
for i in range(n):
  a = int(input())
  l[i] = a
while c <= n:
  next = l[next-1]
  c += 1
  if next == 2:
    print(c)
    break
  elif next in already:
    print(-1)
    break
  already.add(next)

n = int(input())
btn = {b+1: int(input()) for b in range(n)}
cur = 1
cnt = 0
while cnt <= n and cur != 2:
  cur = btn[cur]
  cnt += 1
  if cur == 2:
    print(cnt)
    return
print(-1)

N = int(input())
a = tuple(int(input()) for _ in range(N))
log = set()
i = 0
count = 1
while a[i] != 2:
  i = a[i] - 1
  if i in log:
    print(-1)
    break
  else:
    log.add(i)
    count += 1
else:
  print(count)

import sys
import cProfile
input = sys.stdin.readline
def main():
    n=int(input())
    a=[int(input()) for _ in range(n)]
    l=[1]
    i=0
    aa=1
    while True:
        aa=a[aa-1]
        if aa == 2:
            print(i+1)
            return
        if i>n:
            print(-1)
            return
        i+=1
main()

n = int(input())
a = [0] * n
for i in range(n):
    a[i] = int(input())

now = 1
for i in range(1, n + 10):
    now = a[now - 1]
    if now == 2:
        print(i)
        break
else:
    print((-1))


n = int(input())
a = [int(input()) for i in range(n)]
cnt = 0

if 2 not in a:
  print(-1)
  return

x = 1
for i in range(n-1):
 
  x = a[x-1]
  cnt += 1

            
  if x == 2:
    print(cnt)
    return

print(-1)

N = int(input())

buttons = []
for _ in range(N):
  buttons.append(int(input()))

s = 1
cnt = 0
while True:
  s = buttons[s-1]
  cnt += 1
  if s == 2:
    break
  elif cnt > N:
    cnt = -1
    break

print(cnt)

n = int(input())
data = [int(input()) for i in range(n)]
count = 0
temp = 1
for i in range(n):
    temp = data[temp-1]
    count += 1
    if temp == 2:
        print(count)
        break
else:
    print(-1)

n=int(input())
l=[int(input()) for i in range(n)]

num=l[0]
cnt=0
for i in range(10**5):
  if num==2:
    cnt+=1
    break
  else:
    num=l[num-1]
    cnt+=1
    
if cnt==10**5:
  cnt=-1
  
print(cnt)

N = int(input())
a = []
for i in range(N):
    a.append(int(input()))

x = 1
Sum = 0
while x != 2 and Sum < N:
    x = a[x - 1]
    Sum += 1

if Sum >= N:
    print(-1)
else:
    print(Sum)

n = int(input())
a = [0] * n
for i in range(n):
    a[i] = int(input())

counter = 0
now = 1
visit = [False] * n
visit[0] = True
while True:
    counter += 1
    if a[now - 1] == 2:
        print(counter)
        break
    elif visit[a[now - 1]-1] == True:
        print((-1))
        break
    else:
        visit[a[now - 1]-1] = True
    now = a[now - 1]