n=int(eval(input()))

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

b=a[0]

c=1

ans=-1

for i in range(10**5):

    if b==2:

        ans=c

        break

    b=a[b-1]

    c+=1

print(ans)   
n,*a=list(map(int,open(0).read().split()))

i=ans=1

while a[i-1]!=2 and ans<=n:

    ans+=1

    i=a[i-1]

print((ans if ans<=n else -1))
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
b = a[0]
c = 1
ans = -1
for i in range(10**5):
    if b == 2:
        ans = c
        break
    b = a[b - 1]
    c += 1
print(ans)

n, *a = list(map(int, open(0).read().split()))
i = ans = 1
while a[i - 1] != 2 and ans <= n:
    ans += 1
    i = a[i - 1]
print((ans if ans <= n else -1))

-n = int(eval(input())) -a = [int(eval(input())) for _ in range(n)] -b = a[0] -c = 1 -ans = -1 -for i in range(10**5): -    if b == 2: -        ans = c -        break -    b = a[b - 1] -    c += 1 -print(ans) +n, *a = list(map(int, open(0).read().split())) +i = ans = 1 +while a[i - 1] != 2 and ans <= n: +    ans += 1 +    i = a[i - 1] +print((ans if ans <= n else -1))