a = int(eval(input()))

b = [[int(eval(input())), False]for i in range(a)]

c = 1

d = 0

while True:

    d += 1

    if b[c-1][0] == 2:

        print(d)

        exit()

    else:

        b[c-1][1] = not b[c-1][1]

        if b[c-1][1]:

            c = b[c-1][0]

        else:

            print((-1))

            exit()

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())), False] for i in range(a)]
c = 1
d = 0
while True:
    d += 1
    if b[c - 1][0] == 2:
        print(d)
        exit()
    else:
        b[c - 1][1] = not b[c - 1][1]
        if b[c - 1][1]:
            c = b[c - 1][0]
        else:
            print((-1))
            exit()

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]

-b = [[int(eval(input())), False] for i in range(a)] +b = [int(eval(input())) for _ in range(a)] +e = {} -    if b[c - 1][0] == 2: +    if b[c - 1] == 2: -        b[c - 1][1] = not b[c - 1][1] -        if b[c - 1][1]: -            c = b[c - 1][0] +        if (c - 1) not in e: +            e.setdefault((c - 1), "") +        c = b[c - 1]