N = int(eval(input()))

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

check = [True for _ in range(N)]



ai = 1

count = 1

while check[ai-1]:

  if A[ai-1] == 2:

    print(count)

    exit()

  check[ai-1] = False

  ai = A[ai-1]

  count += 1

print((-1))

N = int(eval(input()))

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

order = A[0]

for i in range(1,N):

  if order == 2:

    print(i)

    exit()

  order = A[order-1]

print((-1))
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
check = [True for _ in range(N)]
ai = 1
count = 1
while check[ai - 1]:
    if A[ai - 1] == 2:
        print(count)
        exit()
    check[ai - 1] = False
    ai = A[ai - 1]
    count += 1
print((-1))

N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
order = A[0]
for i in range(1, N):
    if order == 2:
        print(i)
        exit()
    order = A[order - 1]
print((-1))

-check = [True for _ in range(N)] -ai = 1 -count = 1 -while check[ai - 1]: -    if A[ai - 1] == 2: -        print(count) +order = A[0] +for i in range(1, N): +    if order == 2: +        print(i) -    check[ai - 1] = False -    ai = A[ai - 1] -    count += 1 +    order = A[order - 1]