import time

def main():

    N = int(eval(input()))

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

    start = time.time()

    now = [0] * N

    day = 0

    gamed_day = [-1] * N

    finished = 0

    while True:

        if time.time() - start > 1.6:

            print(((N*(N-1))//2))

            break

        had_game = False

        all_finish = True

        for i in range(N-1, -1, -1):

            if now[i] < N-1 and gamed_day[i] != day:

                all_finish = False

                want = a[i][now[i]] - 1

                if gamed_day[want] != day:# 今日試合していない

                    if a[want][now[want]] - 1 == i:# 相手の試合相手が自分

                        now[want] += 1

                        now[i] += 1

                        if now[want] >= N-1:

                            finished += 1

                        if now[i] >= N-1:

                            finished += 1

                        gamed_day[i] = day

                        gamed_day[want] = day

                        had_game = True

                        # print(day+1, i+1, want+1)

        if all_finish or finished == N:

            if had_game:

                print((day+1))

            else:

                print(day)

            break

        if not had_game:

            print((-1))

            break

        day += 1

    # print(now)



if __name__ == "__main__":

    main()

def main():

    N = int(eval(input()))

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

    

    now = [0] * N

    day = 0

    gamed_day = [-1] * N

    

    ind = [i for i in range(N)]



    while len(ind) > 0:

        next_ = []

        while len(ind) > 0:

            i = ind.pop(-1)

            if now[i] < N-1 and gamed_day[i] != day:

                all_finish = False

                want = a[i][now[i]] - 1

                if gamed_day[want] != day:# 今日試合していない

                    if a[want][now[want]] - 1 == i:# 相手の試合相手が自分

                        now[want] += 1

                        now[i] += 1

                        if now[want] < N-1:

                            next_.append(want)

                        if now[i] < N-1:

                            next_.append(i)

                        gamed_day[i] = day

                        gamed_day[want] = day

                        had_game = True

                        # print(day+1, i+1, want+1)

        ind = next_

        day += 1

    ok = True

    for i in range(N):

        if now[i] < N-1:

            ok = False

            break

    if ok:

        print(day)

    else:

        print((-1))





if __name__ == "__main__":

    main()

import time


def main():
    N = int(eval(input()))
    a = [list(map(int, input().split())) for i in range(N)]
    start = time.time()
    now = [0] * N
    day = 0
    gamed_day = [-1] * N
    finished = 0
    while True:
        if time.time() - start > 1.6:
            print(((N * (N - 1)) // 2))
            break
        had_game = False
        all_finish = True
        for i in range(N - 1, -1, -1):
            if now[i] < N - 1 and gamed_day[i] != day:
                all_finish = False
                want = a[i][now[i]] - 1
                if gamed_day[want] != day:  # 今日試合していない
                    if a[want][now[want]] - 1 == i:  # 相手の試合相手が自分
                        now[want] += 1
                        now[i] += 1
                        if now[want] >= N - 1:
                            finished += 1
                        if now[i] >= N - 1:
                            finished += 1
                        gamed_day[i] = day
                        gamed_day[want] = day
                        had_game = True
                        # print(day+1, i+1, want+1)
        if all_finish or finished == N:
            if had_game:
                print((day + 1))
            else:
                print(day)
            break
        if not had_game:
            print((-1))
            break
        day += 1
    # print(now)


if __name__ == "__main__":
    main()

def main():
    N = int(eval(input()))
    a = [list(map(int, input().split())) for i in range(N)]
    now = [0] * N
    day = 0
    gamed_day = [-1] * N
    ind = [i for i in range(N)]
    while len(ind) > 0:
        next_ = []
        while len(ind) > 0:
            i = ind.pop(-1)
            if now[i] < N - 1 and gamed_day[i] != day:
                all_finish = False
                want = a[i][now[i]] - 1
                if gamed_day[want] != day:  # 今日試合していない
                    if a[want][now[want]] - 1 == i:  # 相手の試合相手が自分
                        now[want] += 1
                        now[i] += 1
                        if now[want] < N - 1:
                            next_.append(want)
                        if now[i] < N - 1:
                            next_.append(i)
                        gamed_day[i] = day
                        gamed_day[want] = day
                        had_game = True
                        # print(day+1, i+1, want+1)
        ind = next_
        day += 1
    ok = True
    for i in range(N):
        if now[i] < N - 1:
            ok = False
            break
    if ok:
        print(day)
    else:
        print((-1))


if __name__ == "__main__":
    main()

-import time - - -    start = time.time() -    finished = 0 -    while True: -        if time.time() - start > 1.6: -            print(((N * (N - 1)) // 2)) -            break -        had_game = False -        all_finish = True -        for i in range(N - 1, -1, -1): +    ind = [i for i in range(N)] +    while len(ind) > 0: +        next_ = [] +        while len(ind) > 0: +            i = ind.pop(-1) -                        if now[want] >= N - 1: -                            finished += 1 -                        if now[i] >= N - 1: -                            finished += 1 +                        if now[want] < N - 1: +                            next_.append(want) +                        if now[i] < N - 1: +                            next_.append(i) -        if all_finish or finished == N: -            if had_game: -                print((day + 1)) -            else: -                print(day) +        ind = next_ +        day += 1 +    ok = True +    for i in range(N): +        if now[i] < N - 1: +            ok = False -        if not had_game: -            print((-1)) -            break -        day += 1 -    # print(now) +    if ok: +        print(day) +    else: +        print((-1))