n = int(eval(input()))

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

if 2 not in a:

    print((-1))

    quit()

else:

    t = a.index(2)

    s = 1

    count = 0

    while count <= 10**6:

        s = a[s-1]

        count += 1

        if s == 2:

            print(count)

            quit()

print((-1))
n = int(eval(input()))

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

if 2 not in a:

    print((-1))

    quit()

else:

    s = 1

    count = 0

    while count <= n:

        s = a[s-1]

        count += 1

        if s == 2:

            print(count)

            quit()

print((-1))
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
if 2 not in a:
    print((-1))
    quit()
else:
    t = a.index(2)
    s = 1
    count = 0
    while count <= 10**6:
        s = a[s - 1]
        count += 1
        if s == 2:
            print(count)
            quit()
print((-1))

n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
if 2 not in a:
    print((-1))
    quit()
else:
    s = 1
    count = 0
    while count <= n:
        s = a[s - 1]
        count += 1
        if s == 2:
            print(count)
            quit()
print((-1))

-    t = a.index(2) -    while count <= 10**6: +    while count <= n: