N=int(eval(input()))

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

count = 0

b=1

for i in range(N+1):

  b = A[b-1][1]

  count+= 1

  if b==2:

    print(count)

    break

if count == N+1:

  print((-1))

    
N=int(eval(input()))

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

count = 0

b=1

for i in range(N+1):

  b = A[b-1]

  count+= 1

  if b==2:

    print(count)

    break

if count == N+1:

  print((-1))
N = int(eval(input()))
A = [((i + 1), int(eval(input()))) for i in range(N)]
count = 0
b = 1
for i in range(N + 1):
    b = A[b - 1][1]
    count += 1
    if b == 2:
        print(count)
        break
if count == N + 1:
    print((-1))

N = int(eval(input()))
A = [(int(eval(input()))) for i in range(N)]
count = 0
b = 1
for i in range(N + 1):
    b = A[b - 1]
    count += 1
    if b == 2:
        print(count)
        break
if count == N + 1:
    print((-1))

-A = [((i + 1), int(eval(input()))) for i in range(N)] +A = [(int(eval(input()))) for i in range(N)] -    b = A[b - 1][1] +    b = A[b - 1]