n=int(eval(input()))

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

x=1

was=set()

now=1

while x==1:

    if now==2:

        print((len(was)))

        break

    if now in was:

        print('-1')

        break

    was|={now,}

    now=data[now-1]

    
N=int(eval(input()))

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

cnt=0

now=1

for i in range(N+1):

    if now==2:

        break

    cnt+=1

    now=A[now-1]

if cnt>N:print((-1))

else:print(cnt)
n = int(eval(input()))
data = [int(eval(input())) for i in range(n)]
x = 1
was = set()
now = 1
while x == 1:
    if now == 2:
        print((len(was)))
        break
    if now in was:
        print("-1")
        break
    was |= {
        now,
    }
    now = data[now - 1]

N = int(eval(input()))
A = [int(eval(input())) for i in range(N)]
cnt = 0
now = 1
for i in range(N + 1):
    if now == 2:
        break
    cnt += 1
    now = A[now - 1]
if cnt > N:
    print((-1))
else:
    print(cnt)

-n = int(eval(input())) -data = [int(eval(input())) for i in range(n)] -x = 1 -was = set() +N = int(eval(input())) +A = [int(eval(input())) for i in range(N)] +cnt = 0 -while x == 1: +for i in range(N + 1): -        print((len(was))) -    if now in was: -        print("-1") -        break -    was |= { -        now, -    } -    now = data[now - 1] +    cnt += 1 +    now = A[now - 1] +if cnt > N: +    print((-1)) +else: +    print(cnt)