n = int(eval(input()))

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

b = [0]*n

b[0] = 1

count = 0

c = a[0]

for i in range(n):

  if b[c-1] == 0:

    b[c-1] = 1

    if c == 2:

      print((count+1))

      break

  else:

    print((-1))

    break

  c = a[c-1]

  count += 1
n = int(eval(input()))

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

c, s = 1, a[0]

while s!= 2 and c<n:

  c, s = c+1, a[s-1]

print((c if c<n else -1))
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
b = [0] * n
b[0] = 1
count = 0
c = a[0]
for i in range(n):
    if b[c - 1] == 0:
        b[c - 1] = 1
        if c == 2:
            print((count + 1))
            break
    else:
        print((-1))
        break
    c = a[c - 1]
    count += 1

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

-b = [0] * n -b[0] = 1 -count = 0 -c = a[0] -for i in range(n): -    if b[c - 1] == 0: -        b[c - 1] = 1 -        if c == 2: -            print((count + 1)) -            break -    else: -        print((-1)) -        break -    c = a[c - 1] -    count += 1 +c, s = 1, a[0] +while s != 2 and c < n: +    c, s = c + 1, a[s - 1] +print((c if c < n else -1))