n = int(eval(input()))

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

A = [0]*n

i = 0

ok = False

cnt = 0

for _ in range(n):

  cnt += 1

  A[i] = 1

  if a[i] == 2: 

    ok = True

    break

  i = a[i]-1

  if A[i] == 1: break

print((cnt if ok else -1))
n = int(eval(input()))

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

check = [0] * (n+1)

now = 1

cnt = 0

while now != 2:

    if check[now] == 1: break

    cnt += 1

    check[now] = 1

    now = a[now]

print((cnt if now == 2 else -1))
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
A = [0] * n
i = 0
ok = False
cnt = 0
for _ in range(n):
    cnt += 1
    A[i] = 1
    if a[i] == 2:
        ok = True
        break
    i = a[i] - 1
    if A[i] == 1:
        break
print((cnt if ok else -1))

n = int(eval(input()))
a = [0] + [int(eval(input())) for _ in range(n)]
check = [0] * (n + 1)
now = 1
cnt = 0
while now != 2:
    if check[now] == 1:
        break
    cnt += 1
    check[now] = 1
    now = a[now]
print((cnt if now == 2 else -1))

-a = [int(eval(input())) for _ in range(n)] -A = [0] * n -i = 0 -ok = False +a = [0] + [int(eval(input())) for _ in range(n)] +check = [0] * (n + 1) +now = 1 -for _ in range(n): +while now != 2: +    if check[now] == 1: +        break -    A[i] = 1 -    if a[i] == 2: -        ok = True -        break -    i = a[i] - 1 -    if A[i] == 1: -        break -print((cnt if ok else -1)) +    check[now] = 1 +    now = a[now] +print((cnt if now == 2 else -1))