n = int(eval(input()))

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

i = a[0]

cnt = 1

while cnt<=n and i!=2:

    i = a[i-1]

    cnt += 1

if cnt==n+1:print((-1))

else:print(cnt)

n = int(eval(input()))

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

used = [False]*n

f = True

cur = 0

cnt = 0

while f:

    cnt += 1

    cur = a[cur]-1

    if cur == 1:

        break

    if used[cur]:

        f = False

        cnt = -1

    else:

        used[cur] = True

print(cnt)
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
i = a[0]
cnt = 1
while cnt <= n and i != 2:
    i = a[i - 1]
    cnt += 1
if cnt == n + 1:
    print((-1))
else:
    print(cnt)

n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
used = [False] * n
f = True
cur = 0
cnt = 0
while f:
    cnt += 1
    cur = a[cur] - 1
    if cur == 1:
        break
    if used[cur]:
        f = False
        cnt = -1
    else:
        used[cur] = True
print(cnt)

-a = [int(eval(input())) for _ in range(n)] -i = a[0] -cnt = 1 -while cnt <= n and i != 2: -    i = a[i - 1] +a = [int(eval(input())) for i in range(n)] +used = [False] * n +f = True +cur = 0 +cnt = 0 +while f: -if cnt == n + 1: -    print((-1)) -else: -    print(cnt) +    cur = a[cur] - 1 +    if cur == 1: +        break +    if used[cur]: +        f = False +        cnt = -1 +    else: +        used[cur] = True +print(cnt)