n = int(eval(input()))

l = [int(eval(input()))-1 for i in range(n)]



a = l[0]

for i in range(n):

  if a == 1:

    print((i+1))

    exit()

  a = l[a]

else:

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

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



g = [[] for _ in range(n)]

for i, a in enumerate(A):

    g[a-1].append(i)



from collections import deque

q = deque([1])

visit = [-1]*n

visit[1] = 0

while q:

    v = q.popleft()

    for u in g[v]:

        if visit[u] == -1:

            visit[u] = visit[v]+1

            q.append(u)

if visit[0] == -1:

    print((-1))

else:

    print((visit[0]))

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

n = int(eval(input()))
A = [int(eval(input())) for _ in range(n)]
g = [[] for _ in range(n)]
for i, a in enumerate(A):
    g[a - 1].append(i)
from collections import deque

q = deque([1])
visit = [-1] * n
visit[1] = 0
while q:
    v = q.popleft()
    for u in g[v]:
        if visit[u] == -1:
            visit[u] = visit[v] + 1
            q.append(u)
if visit[0] == -1:
    print((-1))
else:
    print((visit[0]))

-l = [int(eval(input())) - 1 for i in range(n)] -a = l[0] -for i in range(n): -    if a == 1: -        print((i + 1)) -        exit() -    a = l[a] +A = [int(eval(input())) for _ in range(n)] +g = [[] for _ in range(n)] +for i, a in enumerate(A): +    g[a - 1].append(i) +from collections import deque + +q = deque([1]) +visit = [-1] * n +visit[1] = 0 +while q: +    v = q.popleft() +    for u in g[v]: +        if visit[u] == -1: +            visit[u] = visit[v] + 1 +            q.append(u) +if visit[0] == -1: +    print((-1)) -    print((-1)) +    print((visit[0]))