# 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])

    ans, cnt, matched = 0, 0, set()

    q = [i for i in range(1, N + 1)]

    while q:

        ans += 1

        next_q = []

        for i in q:

            if i in B and i not in matched:

                x = B[i][0]

                if x in B and x not in matched and B[x][0] == i:

                    B[i].popleft(), B[x].popleft()

                    matched |= {i, x}

                    cnt += 1

                    if not B[i]:

                        del B[i]

                    else:

                        next_q += [i]

                    if not B[x]:

                        del B[x]

                    else:

                        next_q += [x]

        q = next_q

        matched = set()

    flg = cnt == N * (N - 1) // 2

    print((ans 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, *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])
    ans, cnt, matched = 0, 0, set()
    q = [i for i in range(1, N + 1)]
    while q:
        ans += 1
        next_q = []
        for i in q:
            if i in B and i not in matched:
                x = B[i][0]
                if x in B and x not in matched and B[x][0] == i:
                    B[i].popleft(), B[x].popleft()
                    matched |= {i, x}
                    cnt += 1
                    if not B[i]:
                        del B[i]
                    else:
                        next_q += [i]
                    if not B[x]:
                        del B[x]
                    else:
                        next_q += [x]
        q = next_q
        matched = set()
    flg = cnt == N * (N - 1) // 2
    print((ans 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()

-    ans, cnt, matched = 0, 0, set() -    q = [i for i in range(1, N + 1)] +    cnt, cur, days = 0, [0] * (N + 1), [0] * (N + 1) +    q = deque(list(range(1, N + 1))) -        ans += 1 -        next_q = [] -        for i in q: -            if i in B and i not in matched: -                x = B[i][0] -                if x in B and x not in matched and B[x][0] == i: -                    B[i].popleft(), B[x].popleft() -                    matched |= {i, x} -                    cnt += 1 -                    if not B[i]: -                        del B[i] -                    else: -                        next_q += [i] -                    if not B[x]: -                        del B[x] -                    else: -                        next_q += [x] -        q = next_q -        matched = set() -    flg = cnt == N * (N - 1) // 2 -    print((ans if flg else -1)) +        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))