import sys

N = int(eval(input()))

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

d = [0] * N

b = 0

c = 0

while True:

    if b == 1:

        print(c)

        sys.exit()

    c += 1

    b = a[b] - 1

    if d[b] == 1:

        print('-1')

        sys.exit()

    d[b] = 1

N = int(eval(input()))

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

s = [0] * N

c = 0

OK = False

n = 0

while True:

    c += 1

    n = a[n] - 1

    if n == 1:

        OK = True

        break

    if s[n] == 1:

        break

    s[n] = 1

if OK:

    print(c)

else:

    print((-1))
import sys

N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
d = [0] * N
b = 0
c = 0
while True:
    if b == 1:
        print(c)
        sys.exit()
    c += 1
    b = a[b] - 1
    if d[b] == 1:
        print("-1")
        sys.exit()
    d[b] = 1

N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
s = [0] * N
c = 0
OK = False
n = 0
while True:
    c += 1
    n = a[n] - 1
    if n == 1:
        OK = True
        break
    if s[n] == 1:
        break
    s[n] = 1
if OK:
    print(c)
else:
    print((-1))

-import sys - -a = [int(eval(input())) for i in range(N)] -d = [0] * N -b = 0 +a = [int(eval(input())) for _ in range(N)] +s = [0] * N +OK = False +n = 0 -    if b == 1: -        print(c) -        sys.exit() -    b = a[b] - 1 -    if d[b] == 1: -        print("-1") -        sys.exit() -    d[b] = 1 +    n = a[n] - 1 +    if n == 1: +        OK = True +        break +    if s[n] == 1: +        break +    s[n] = 1 +if OK: +    print(c) +else: +    print((-1))