a = int(eval(input()))

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

c = 1

d = 0

e = {}

while True:

    d += 1

    if b[c-1] == 2:

        print(d)

        exit()

    else:

        if (c-1) not in e:

            e.setdefault((c-1), "")

        else:

            print((-1))

            exit()

        c = b[c - 1]

a = int(eval(input()))

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

c = 0

d = 1

while True:

    if b[d-1] == 2:

        c += 1

        break

    elif c > a:

        print((-1))

        exit()

    else:

        d = b[d-1]

        c += 1

print(c)

a = int(eval(input()))
b = [int(eval(input())) for _ in range(a)]
c = 1
d = 0
e = {}
while True:
    d += 1
    if b[c - 1] == 2:
        print(d)
        exit()
    else:
        if (c - 1) not in e:
            e.setdefault((c - 1), "")
        else:
            print((-1))
            exit()
        c = b[c - 1]

a = int(eval(input()))
b = [int(eval(input())) for _ in range(a)]
c = 0
d = 1
while True:
    if b[d - 1] == 2:
        c += 1
        break
    elif c > a:
        print((-1))
        exit()
    else:
        d = b[d - 1]
        c += 1
print(c)

-c = 1 -d = 0 -e = {} +c = 0 +d = 1 -    d += 1 -    if b[c - 1] == 2: -        print(d) +    if b[d - 1] == 2: +        c += 1 +        break +    elif c > a: +        print((-1)) -        if (c - 1) not in e: -            e.setdefault((c - 1), "") -        else: -            print((-1)) -            exit() -        c = b[c - 1] +        d = b[d - 1] +        c += 1 +print(c)