n=int(eval(input()))

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



count=0

flag=False

push_num=1



for i in range(n):

  count+=1

  light_num=l[push_num-1]

  if light_num==2:

    flag=True

    break

  push_num=light_num

if flag:

  print(count)

else:

  print((-1))

n = int(eval(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(eval(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(eval(input()))
l = [int(eval(input())) for i in range(n)]
count = 0
flag = False
push_num = 1
for i in range(n):
    count += 1
    light_num = l[push_num - 1]
    if light_num == 2:
        flag = True
        break
    push_num = light_num
if flag:
    print(count)
else:
    print((-1))

n = int(eval(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(eval(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)

-l = [int(eval(input())) for i in range(n)] -count = 0 -flag = False -push_num = 1 +# 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 = [] -    count += 1 -    light_num = l[push_num - 1] -    if light_num == 2: -        flag = True +    a = int(eval(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 -    push_num = light_num -if flag: -    print(count) -else: -    print((-1)) +print(ans)