import collections





def slove():

    import sys

    import collections

    input = sys.stdin.readline

    n = int(input().rstrip('\n'))

    a = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]

    for i in range(n):

        for j in range(n-1):

            a[i][j] -= 1

    ad = [0] * n

    q = collections.deque([[0, i] for i in range(n)])

    for i in range(10 ** 20):

        fq = collections.defaultdict(list)

        while True:

            if len(q) != 0:

                d, p = q.popleft()

                if d == i:

                    if p not in fq:

                        tp = a[p][ad[p]]

                        if tp not in fq:

                            if p == a[tp][ad[tp]]:

                                fq[tp]

                                fq[p]

                                ad[tp] += 1

                                ad[p] += 1

                                if ad[tp] < n - 1:

                                    q.append([d+1, tp])

                                if ad[p] < n - 1:

                                    q.append([d+1, p])

                else:

                    q.appendleft([d, p])

                    break

            else:

                if sum(ad) == n * (n - 1):

                    print((i + 1))

                else:

                    print((-1))

                exit()





if __name__ == '__main__':

    slove()

import sys

import collections





def solve():

    readline = sys.stdin.buffer.readline

    mod = 10 ** 9 + 7

    n = int(readline())

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

    for i in range(n):

        for j in range(n - 1):

            a[i][j] -= 1

    ls = [0] * n

    ql = collections.deque()

    for i in range(n):

        ql.append([0, i])



    for i in range(10 ** 10):

        fq = collections.defaultdict(list)

        while True:

            if len(ql) != 0:

                d, ps = ql.popleft()

                if d == i:

                    if ps not in fq:

                        nw = a[ps][ls[ps]]

                        if nw not in fq:

                            rw = a[nw][ls[nw]]

                            if ps == rw:

                                fq[ps]

                                fq[nw]

                                ls[ps] += 1

                                ls[nw] += 1

                                if ls[ps] < n - 1:

                                    ql.append([d+1, ps])

                                if ls[nw] < n - 1:

                                    ql.append([d+1, nw])

                else:

                    ql.appendleft([d, ps])

                    break

            else:

                if sum(ls) == n * (n - 1):

                    print((i + 1))

                else:

                    print((-1))

                exit()





if __name__ == '__main__':

    solve()

import collections


def slove():
    import sys
    import collections

    input = sys.stdin.readline
    n = int(input().rstrip("\n"))
    a = [list(map(int, input().rstrip("\n").split())) for _ in range(n)]
    for i in range(n):
        for j in range(n - 1):
            a[i][j] -= 1
    ad = [0] * n
    q = collections.deque([[0, i] for i in range(n)])
    for i in range(10**20):
        fq = collections.defaultdict(list)
        while True:
            if len(q) != 0:
                d, p = q.popleft()
                if d == i:
                    if p not in fq:
                        tp = a[p][ad[p]]
                        if tp not in fq:
                            if p == a[tp][ad[tp]]:
                                fq[tp]
                                fq[p]
                                ad[tp] += 1
                                ad[p] += 1
                                if ad[tp] < n - 1:
                                    q.append([d + 1, tp])
                                if ad[p] < n - 1:
                                    q.append([d + 1, p])
                else:
                    q.appendleft([d, p])
                    break
            else:
                if sum(ad) == n * (n - 1):
                    print((i + 1))
                else:
                    print((-1))
                exit()


if __name__ == "__main__":
    slove()

import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10**9 + 7
    n = int(readline())
    a = [list(map(int, readline().split())) for _ in range(n)]
    for i in range(n):
        for j in range(n - 1):
            a[i][j] -= 1
    ls = [0] * n
    ql = collections.deque()
    for i in range(n):
        ql.append([0, i])
    for i in range(10**10):
        fq = collections.defaultdict(list)
        while True:
            if len(ql) != 0:
                d, ps = ql.popleft()
                if d == i:
                    if ps not in fq:
                        nw = a[ps][ls[ps]]
                        if nw not in fq:
                            rw = a[nw][ls[nw]]
                            if ps == rw:
                                fq[ps]
                                fq[nw]
                                ls[ps] += 1
                                ls[nw] += 1
                                if ls[ps] < n - 1:
                                    ql.append([d + 1, ps])
                                if ls[nw] < n - 1:
                                    ql.append([d + 1, nw])
                else:
                    ql.appendleft([d, ps])
                    break
            else:
                if sum(ls) == n * (n - 1):
                    print((i + 1))
                else:
                    print((-1))
                exit()


if __name__ == "__main__":
    solve()

+import sys -def slove(): -    import sys -    import collections - -    input = sys.stdin.readline -    n = int(input().rstrip("\n")) -    a = [list(map(int, input().rstrip("\n").split())) for _ in range(n)] +def solve(): +    readline = sys.stdin.buffer.readline +    mod = 10**9 + 7 +    n = int(readline()) +    a = [list(map(int, readline().split())) for _ in range(n)] -    ad = [0] * n -    q = collections.deque([[0, i] for i in range(n)]) -    for i in range(10**20): +    ls = [0] * n +    ql = collections.deque() +    for i in range(n): +        ql.append([0, i]) +    for i in range(10**10): -            if len(q) != 0: -                d, p = q.popleft() +            if len(ql) != 0: +                d, ps = ql.popleft() -                    if p not in fq: -                        tp = a[p][ad[p]] -                        if tp not in fq: -                            if p == a[tp][ad[tp]]: -                                fq[tp] -                                fq[p] -                                ad[tp] += 1 -                                ad[p] += 1 -                                if ad[tp] < n - 1: -                                    q.append([d + 1, tp]) -                                if ad[p] < n - 1: -                                    q.append([d + 1, p]) +                    if ps not in fq: +                        nw = a[ps][ls[ps]] +                        if nw not in fq: +                            rw = a[nw][ls[nw]] +                            if ps == rw: +                                fq[ps] +                                fq[nw] +                                ls[ps] += 1 +                                ls[nw] += 1 +                                if ls[ps] < n - 1: +                                    ql.append([d + 1, ps]) +                                if ls[nw] < n - 1: +                                    ql.append([d + 1, nw]) -                    q.appendleft([d, p]) +                    ql.appendleft([d, ps]) -                if sum(ad) == n * (n - 1): +                if sum(ls) == n * (n - 1): -    slove() +    solve()