import math

import collections

import heapq

from collections import defaultdict

import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)





def main():

    n = int(eval(input()))

    maxv = n*(n-1)//2

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

    for i in range(n):

        for j in range(n-1):

            a[i][j] -= 1



    # 試合を頂点とする(ex 1-2,2-4 : i < j)

    v = 0  # 頂点数

    # 頂点にIDをふる

    Id = [[-1 for i in range(n)] for j in range(n)]

    for i in range(n):

        for j in range(n):

            if i < j:  # 3-1はダメ

                Id[i][j] = v

                Id[j][i] = v

                v += 1





    e = [[] for i in range(maxv)]

    for i in range(n):

        for j in range(n - 1):

            # a[i][j]にIDを代入する Id[1][3]=1 => a[1][3] = a[3][1] = 1

            # x = i

            # y = a[i][j]

            # if x > y:

            #     x, y = y, x

            # a[i][j] = Id[x][y]  # toId(i, a[i][j])

            a[i][j] = Id[i][a[i][j]]

        for j in range(n - 2):

            e[a[i][j + 1]].append(a[i][j])





    calculated = [False for i in range(maxv)]

    dp = [-1 for i in range(maxv)]



    def dfs(v):

        if dp[v] != -1:  # サイクル検知

            if not calculated[v]:

                return -1

            return dp[v]

        dp[v] = 1

        for u in e[v]:

            ret = dfs(u)

            if ret == -1:

                return -1

            dp[v] = max(dp[v], ret + 1)

        calculated[v] = True

        return dp[v]



    ans = 0

    for i in range(v):

        ret = dfs(i)

        if ret == -1:  # detected cycle

            print((-1))

            exit()

        ans = max(ans, ret)

    print(ans)





if __name__ == '__main__':

    main()

import math

import collections

import heapq

from collections import defaultdict

import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)





def main():

    n = int(eval(input()))

    maxv = n*(n-1)//2

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

    for i in range(n):

        for j in range(n-1):

            a[i][j] -= 1



    # 試合を頂点とする(ex 1-2,2-4 : i < j)

    v = 0  # 頂点数

    # 頂点にIDをふる

    Id = [[-1 for i in range(n)] for j in range(n)]

    for i in range(n):

        for j in range(n):

            if i < j:

                Id[i][j] = v

                Id[j][i] = v

                v += 1





    # 頂点間の辺を貼る

    e = [[] for i in range(maxv)]

    for i in range(n):

        # a[i][j]にIDを代入する Id[1][3]=1 => a[1][3] = a[3][1] = 1

        for j in range(n - 1):

            a[i][j] = Id[i][a[i][j]]



        # 逆向きに辺をはる

        for j in range(n - 2):

            e[a[i][j]].append(a[i][j+1])







    calculated = [False for i in range(maxv)]

    dp = [-1 for i in range(maxv)]



    def dfs(v):

        if dp[v] != -1:  # サイクル検知

            if not calculated[v]:

                return -1

            return dp[v]

        dp[v] = 1

        for u in e[v]:

            ret = dfs(u)

            if ret == -1:

                return -1

            dp[v] = max(dp[v], ret + 1)

        calculated[v] = True

        return dp[v]



    ans = 0

    for i in range(v):

        ret = dfs(i)

        if ret == -1:  # サイクル検知

            print((-1))

            exit()

        ans = max(ans, ret)

    print(ans)





if __name__ == '__main__':

    main()

import math
import collections
import heapq
from collections import defaultdict
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)


def main():
    n = int(eval(input()))
    maxv = n * (n - 1) // 2
    a = [list(map(int, input().split())) for i in range(n)]
    for i in range(n):
        for j in range(n - 1):
            a[i][j] -= 1
    # 試合を頂点とする(ex 1-2,2-4 : i < j)
    v = 0  # 頂点数
    # 頂点にIDをふる
    Id = [[-1 for i in range(n)] for j in range(n)]
    for i in range(n):
        for j in range(n):
            if i < j:  # 3-1はダメ
                Id[i][j] = v
                Id[j][i] = v
                v += 1
    e = [[] for i in range(maxv)]
    for i in range(n):
        for j in range(n - 1):
            # a[i][j]にIDを代入する Id[1][3]=1 => a[1][3] = a[3][1] = 1
            # x = i
            # y = a[i][j]
            # if x > y:
            #     x, y = y, x
            # a[i][j] = Id[x][y]  # toId(i, a[i][j])
            a[i][j] = Id[i][a[i][j]]
        for j in range(n - 2):
            e[a[i][j + 1]].append(a[i][j])
    calculated = [False for i in range(maxv)]
    dp = [-1 for i in range(maxv)]

    def dfs(v):
        if dp[v] != -1:  # サイクル検知
            if not calculated[v]:
                return -1
            return dp[v]
        dp[v] = 1
        for u in e[v]:
            ret = dfs(u)
            if ret == -1:
                return -1
            dp[v] = max(dp[v], ret + 1)
        calculated[v] = True
        return dp[v]

    ans = 0
    for i in range(v):
        ret = dfs(i)
        if ret == -1:  # detected cycle
            print((-1))
            exit()
        ans = max(ans, ret)
    print(ans)


if __name__ == "__main__":
    main()

import math
import collections
import heapq
from collections import defaultdict
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)


def main():
    n = int(eval(input()))
    maxv = n * (n - 1) // 2
    a = [list(map(int, input().split())) for i in range(n)]
    for i in range(n):
        for j in range(n - 1):
            a[i][j] -= 1
    # 試合を頂点とする(ex 1-2,2-4 : i < j)
    v = 0  # 頂点数
    # 頂点にIDをふる
    Id = [[-1 for i in range(n)] for j in range(n)]
    for i in range(n):
        for j in range(n):
            if i < j:
                Id[i][j] = v
                Id[j][i] = v
                v += 1
    # 頂点間の辺を貼る
    e = [[] for i in range(maxv)]
    for i in range(n):
        # a[i][j]にIDを代入する Id[1][3]=1 => a[1][3] = a[3][1] = 1
        for j in range(n - 1):
            a[i][j] = Id[i][a[i][j]]
        # 逆向きに辺をはる
        for j in range(n - 2):
            e[a[i][j]].append(a[i][j + 1])
    calculated = [False for i in range(maxv)]
    dp = [-1 for i in range(maxv)]

    def dfs(v):
        if dp[v] != -1:  # サイクル検知
            if not calculated[v]:
                return -1
            return dp[v]
        dp[v] = 1
        for u in e[v]:
            ret = dfs(u)
            if ret == -1:
                return -1
            dp[v] = max(dp[v], ret + 1)
        calculated[v] = True
        return dp[v]

    ans = 0
    for i in range(v):
        ret = dfs(i)
        if ret == -1:  # サイクル検知
            print((-1))
            exit()
        ans = max(ans, ret)
    print(ans)


if __name__ == "__main__":
    main()

-            if i < j:  # 3-1はダメ +            if i < j: +    # 頂点間の辺を貼る +        # a[i][j]にIDを代入する Id[1][3]=1 => a[1][3] = a[3][1] = 1 -            # a[i][j]にIDを代入する Id[1][3]=1 => a[1][3] = a[3][1] = 1 -            # x = i -            # y = a[i][j] -            # if x > y: -            #     x, y = y, x -            # a[i][j] = Id[x][y]  # toId(i, a[i][j]) +        # 逆向きに辺をはる -            e[a[i][j + 1]].append(a[i][j]) +            e[a[i][j]].append(a[i][j + 1]) -        if ret == -1:  # detected cycle +        if ret == -1:  # サイクル検知