output_description: If it is possible to schedule all the matches so that all of the conditions
  are satisfied, print the minimum number of days required; if it is impossible,
  print `-1`.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
      A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
      :
      A_{N, 1} A_{N, 2} \ldots A_{N, N-1}
attempt: import sys
  
  # from collections import deque
  input = sys.stdin.readline
  
  
  def main():
      n = int(input())
      v = n * (n - 1) // 2
      A = [list(map(int, input().split())) for _ in range(n)]
  
      # 頂点と i vs j との対応は (i < j)
      # (n-j+i)(n-j+i-1)//2 + i
  
      path = [set() for _ in range(v)]
  
      def vertex(vs1, vs2):
          m, M = sorted([vs1, vs2])
          return (n - M + m) * (n - M + m - 1) // 2 + m
  
      parent = set(range(v))
  
      for i in range(n):
          for j in range(1, n - 1):
              vs = vertex(i, A[i][j] - 1)
              vsp = vertex(i, A[i][j - 1] - 1)
              path[vs].add(vsp)
              if vsp in parent:
                  parent.remove(vsp)
  
      def dfs(start):
          if memo[start] != -1:
              return memo[start]
          # que = deque([(start,1)])
          # ret = 0
          # while que:
          #     p,d = que.pop()
          #     for nxt in path[p]:
          #         ret = max(ret,d+1)
          #         que.append((nxt,d+1))
          ret = 1
          for nxt in path[start]:
              ret = max(ret, dfs(nxt) + 1)
          memo[start] = ret
          return ret
  
      ans = -1
      memo = [-1] * v
      for i in parent:
          ans = max(ans, dfs(i))
      print(ans)
  
  
  main()
  
problem_description: Statement
  
  N players will participate in a tennis tournament. We will call them Player 1,
  Player 2, \ldots, Player N.
  
  The tournament is round-robin format, and there will be N(N-1)/2 matches in
  total. Is it possible to schedule these matches so that all of the following
  conditions are satisfied? If the answer is yes, also find the minimum number
  of days required.
  
    * Each player plays at most one matches in a day.
    * Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.
samples: [{"input": "3\n    2 3\n    1 3\n    1 2", "output": "3\n    \n\nAll the conditions can be satisfied if the matches are scheduled for three\ndays as follows:\n\n  * Day 1: Player 1 vs Player 2\n  * Day 2: Player 1 vs Player 3\n  * Day 3: Player 2 vs Player 3\n\nThis is the minimum number of days required.\n\n* * *"}, {"input": "4\n    2 3 4\n    1 3 4\n    4 1 2\n    3 1 2", "output": "4\n    \n\nAll the conditions can be satisfied if the matches are scheduled for four days\nas follows:\n\n  * Day 1: Player 1 vs Player 2, Player 3 vs Player 4\n  * Day 2: Player 1 vs Player 3\n  * Day 3: Player 1 vs Player 4, Player 2 vs Player 3\n  * Day 4: Player 2 vs Player 4\n\nThis is the minimum number of days required.\n\n* * *"}, {"input": "3\n    2 3\n    3 1\n    1 2", "output": "-1\n    \n\nAny scheduling of the matches violates some condition."}]