n = int(eval(input()))

a = [0] * n

for i in range(n):

    a[i] = int(eval(input()))



counter = 0

now = 1

visit = [False] * n

visit[0] = True

while True:

    counter += 1

    if a[now - 1] == 2:

        print(counter)

        break

    elif visit[a[now - 1]-1] == True:

        print((-1))

        break

    else:

        visit[a[now - 1]-1] = True

    now = a[now - 1]

n = int(eval(input()))

a = [0] * n

for i in range(n):

    a[i] = int(eval(input()))



visit = set()

now = 1

for i in range(1, n + 10):

    if now not in visit:

        visit.add(now)

        now = a[now - 1]

    else:

        print((-1))

        break

    if now == 2:

        print(i)

        break

n = int(eval(input()))
a = [0] * n
for i in range(n):
    a[i] = int(eval(input()))
counter = 0
now = 1
visit = [False] * n
visit[0] = True
while True:
    counter += 1
    if a[now - 1] == 2:
        print(counter)
        break
    elif visit[a[now - 1] - 1] == True:
        print((-1))
        break
    else:
        visit[a[now - 1] - 1] = True
    now = a[now - 1]

n = int(eval(input()))
a = [0] * n
for i in range(n):
    a[i] = int(eval(input()))
visit = set()
now = 1
for i in range(1, n + 10):
    if now not in visit:
        visit.add(now)
        now = a[now - 1]
    else:
        print((-1))
        break
    if now == 2:
        print(i)
        break

-counter = 0 +visit = set() -visit = [False] * n -visit[0] = True -while True: -    counter += 1 -    if a[now - 1] == 2: -        print(counter) -        break -    elif visit[a[now - 1] - 1] == True: +for i in range(1, n + 10): +    if now not in visit: +        visit.add(now) +        now = a[now - 1] +    else: -    else: -        visit[a[now - 1] - 1] = True -    now = a[now - 1] +    if now == 2: +        print(i) +        break