n = int(eval(input()))

a = []

for i in range(n):

  a.append(int(eval(input())))



visited = set()

count = 0

next = 1

while True:

  count += 1

  visited.add(next)

  next = a[next-1]

  if next == 2:

    print(count)

    exit(0)

  if next in visited:

    print((-1))

    exit(0)
n = int(eval(input()))

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



b = a[0]

cnt = 1



while b != 2 and cnt <= n:

    b = a[b-1]

    cnt += 1

if b == 2:

    print(cnt)

else:

    print((-1))

n = int(eval(input()))
a = []
for i in range(n):
    a.append(int(eval(input())))
visited = set()
count = 0
next = 1
while True:
    count += 1
    visited.add(next)
    next = a[next - 1]
    if next == 2:
        print(count)
        exit(0)
    if next in visited:
        print((-1))
        exit(0)

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

-a = [] -for i in range(n): -    a.append(int(eval(input()))) -visited = set() -count = 0 -next = 1 -while True: -    count += 1 -    visited.add(next) -    next = a[next - 1] -    if next == 2: -        print(count) -        exit(0) -    if next in visited: -        print((-1)) -        exit(0) +a = [int(eval(input())) for i in range(n)] +b = a[0] +cnt = 1 +while b != 2 and cnt <= n: +    b = a[b - 1] +    cnt += 1 +if b == 2: +    print(cnt) +else: +    print((-1))