N = int(eval(input()))

A = [list([int(x) - 1 for x in input().split()]) + [-1] for i in range(N)]



ans = 0

matched = 0

marker = [0] * N

que = list(range(N))



while matched < N * (N - 1) // 2:

    already_matched = set()

    no_matched = True

    next_que = []



    while que:

        player = que.pop()

        partner = A[player][marker[player]]



        if A[partner][marker[partner]] == player:

            if not ((player not in already_matched) and (partner not in already_matched)):

                continue



            matched += 1



            marker[player] += 1

            marker[partner] += 1



            already_matched.add(player)

            already_matched.add(partner)



            next_que.append(player)

            next_que.append(partner)



            no_matched = False



    ans += 1

    que = next_que[:]

    if no_matched:

        print((-1))

        break



else:

    print(ans)

N = int(eval(input()))

A = [list([int(x) - 1 for x in input().split()]) + [-1] for i in range(N)]



ans = 0

matched = 0

marker = [0] * N

que = list(range(N))



while matched < N * (N - 1) // 2:

    already_matched = set()

    next_que = []



    while que:

        player = que.pop()

        partner = A[player][marker[player]]



        if A[partner][marker[partner]] == player:

            if not ((player not in already_matched) and (partner not in already_matched)):

                continue



            matched += 1



            marker[player] += 1

            marker[partner] += 1



            already_matched.add(player)

            already_matched.add(partner)



            next_que.append(player)

            next_que.append(partner)



    if not next_que:

        print((-1))

        break



    ans += 1

    que = next_que[:]



else:

    print(ans)

N = int(eval(input()))
A = [list([int(x) - 1 for x in input().split()]) + [-1] for i in range(N)]
ans = 0
matched = 0
marker = [0] * N
que = list(range(N))
while matched < N * (N - 1) // 2:
    already_matched = set()
    no_matched = True
    next_que = []
    while que:
        player = que.pop()
        partner = A[player][marker[player]]
        if A[partner][marker[partner]] == player:
            if not (
                (player not in already_matched) and (partner not in already_matched)
            ):
                continue
            matched += 1
            marker[player] += 1
            marker[partner] += 1
            already_matched.add(player)
            already_matched.add(partner)
            next_que.append(player)
            next_que.append(partner)
            no_matched = False
    ans += 1
    que = next_que[:]
    if no_matched:
        print((-1))
        break
else:
    print(ans)

N = int(eval(input()))
A = [list([int(x) - 1 for x in input().split()]) + [-1] for i in range(N)]
ans = 0
matched = 0
marker = [0] * N
que = list(range(N))
while matched < N * (N - 1) // 2:
    already_matched = set()
    next_que = []
    while que:
        player = que.pop()
        partner = A[player][marker[player]]
        if A[partner][marker[partner]] == player:
            if not (
                (player not in already_matched) and (partner not in already_matched)
            ):
                continue
            matched += 1
            marker[player] += 1
            marker[partner] += 1
            already_matched.add(player)
            already_matched.add(partner)
            next_que.append(player)
            next_que.append(partner)
    if not next_que:
        print((-1))
        break
    ans += 1
    que = next_que[:]
else:
    print(ans)

-    no_matched = True -            no_matched = False +    if not next_que: +        print((-1)) +        break -    if no_matched: -        print((-1)) -        break