n=int(eval(input()))

a=[]

for _ in range(n):

    a.append(int(eval(input())))



t=0

for i in range(1,n+1):

    t=a[t]-1

    if t==1:

        print(i)

        break

else:

    print((-1))
n=int(eval(input()))

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

b=0

for i in range(1,n+1):

    b=a[b]-1

    if b==1:

        print(i)

        exit()

else:

    print((-1))
n = int(eval(input()))
a = []
for _ in range(n):
    a.append(int(eval(input())))
t = 0
for i in range(1, n + 1):
    t = a[t] - 1
    if t == 1:
        print(i)
        break
else:
    print((-1))

n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
b = 0
for i in range(1, n + 1):
    b = a[b] - 1
    if b == 1:
        print(i)
        exit()
else:
    print((-1))

-a = [] -for _ in range(n): -    a.append(int(eval(input()))) -t = 0 +a = [int(eval(input())) for _ in range(n)] +b = 0 -    t = a[t] - 1 -    if t == 1: +    b = a[b] - 1 +    if b == 1: -        break +        exit()