n = int(eval(input()))

a = [list(map(int, input().split())) for i in range(n)]

a = [e for row in a for e in row]

ans = 0

i = 0

while n > 0:

    ans += 1

    if a[i] == 2:

        print(ans)

        quit()

    else:

        i = a[i]-1

    n -= 1

print((-1))

n = int(eval(input()))

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



ans = 0

i = 0

while n > 0:

    ans += 1

    if a[i] == 2:

        print(ans)

        quit()

    else:

        i = a[i]-1

    n -= 1

print((-1))

n = int(eval(input()))
a = [list(map(int, input().split())) for i in range(n)]
a = [e for row in a for e in row]
ans = 0
i = 0
while n > 0:
    ans += 1
    if a[i] == 2:
        print(ans)
        quit()
    else:
        i = a[i] - 1
    n -= 1
print((-1))

n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
ans = 0
i = 0
while n > 0:
    ans += 1
    if a[i] == 2:
        print(ans)
        quit()
    else:
        i = a[i] - 1
    n -= 1
print((-1))

-a = [list(map(int, input().split())) for i in range(n)] -a = [e for row in a for e in row] +a = [int(eval(input())) for i in range(n)]