# ABC139E - League

from collections import deque





def main():

    N = int(eval(input()))

    A = [0] + list(deque(list(map(int, input().split()))) for _ in range(N))

    cnt, cur, days = 0, [0] * (N + 1), [0] * (N + 1)

    q = deque(list(range(1, N + 1)))

    while q:

        x = q.popleft()

        if not A[x]:  # player x has finished all games

            continue

        y = A[x].popleft()  # the opponent of x

        if cur[y] == x:  # player y's next game is against x and vice versa

            cnt += 1

            days[x] = days[y] = max(days[x], days[y]) + 1  # day of playing the game

            cur[x], cur[y] = 0, 0  # no next game is currently planned

            q.append(x), q.append(y)

        else:  # the opponent will do another game before playing against x

            cur[x] = y

    flg = cnt == N * (N - 1) // 2  # finished all scheduled games or not

    print((max(days) if flg else -1))





if __name__ == "__main__":

    main()
# ABC139E - League

from collections import deque





def main():

    N, *A = list(map(int, open(0).read().split()))

    B = {}

    for i in range(N):

        x = i * (N - 1)

        B[i + 1] = deque(A[x : x + N - 1])

    cnt, cur, days = 0, [0] * (N + 1), [0] * (N + 1)

    q = deque(list(range(1, N + 1)))

    while q:

        x = q.popleft()

        if not B[x]:  # player x has finished all games

            continue

        y = B[x].popleft()

        if cur[y] == x:  # player y's next game is against x and vice versa

            cnt += 1

            days[x] = days[y] = max(days[x], days[y]) + 1

            cur[x], cur[y] = 0, 0

            q.append(x), q.append(y)

        else:  # the opponent will do another game before playing against x

            cur[x] = y

    flg = cnt == N * (N - 1) // 2  # finished all scheduled games or not

    print((max(days) if flg else -1))





if __name__ == "__main__":

    main()

# ABC139E - League
from collections import deque


def main():
    N = int(eval(input()))
    A = [0] + list(deque(list(map(int, input().split()))) for _ in range(N))
    cnt, cur, days = 0, [0] * (N + 1), [0] * (N + 1)
    q = deque(list(range(1, N + 1)))
    while q:
        x = q.popleft()
        if not A[x]:  # player x has finished all games
            continue
        y = A[x].popleft()  # the opponent of x
        if cur[y] == x:  # player y's next game is against x and vice versa
            cnt += 1
            days[x] = days[y] = max(days[x], days[y]) + 1  # day of playing the game
            cur[x], cur[y] = 0, 0  # no next game is currently planned
            q.append(x), q.append(y)
        else:  # the opponent will do another game before playing against x
            cur[x] = y
    flg = cnt == N * (N - 1) // 2  # finished all scheduled games or not
    print((max(days) if flg else -1))


if __name__ == "__main__":
    main()

# ABC139E - League
from collections import deque


def main():
    N, *A = list(map(int, open(0).read().split()))
    B = {}
    for i in range(N):
        x = i * (N - 1)
        B[i + 1] = deque(A[x : x + N - 1])
    cnt, cur, days = 0, [0] * (N + 1), [0] * (N + 1)
    q = deque(list(range(1, N + 1)))
    while q:
        x = q.popleft()
        if not B[x]:  # player x has finished all games
            continue
        y = B[x].popleft()
        if cur[y] == x:  # player y's next game is against x and vice versa
            cnt += 1
            days[x] = days[y] = max(days[x], days[y]) + 1
            cur[x], cur[y] = 0, 0
            q.append(x), q.append(y)
        else:  # the opponent will do another game before playing against x
            cur[x] = y
    flg = cnt == N * (N - 1) // 2  # finished all scheduled games or not
    print((max(days) if flg else -1))


if __name__ == "__main__":
    main()

-    N = int(eval(input())) -    A = [0] + list(deque(list(map(int, input().split()))) for _ in range(N)) +    N, *A = list(map(int, open(0).read().split())) +    B = {} +    for i in range(N): +        x = i * (N - 1) +        B[i + 1] = deque(A[x : x + N - 1]) -        if not A[x]:  # player x has finished all games +        if not B[x]:  # player x has finished all games -        y = A[x].popleft()  # the opponent of x +        y = B[x].popleft() -            days[x] = days[y] = max(days[x], days[y]) + 1  # day of playing the game -            cur[x], cur[y] = 0, 0  # no next game is currently planned +            days[x] = days[y] = max(days[x], days[y]) + 1 +            cur[x], cur[y] = 0, 0