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: # cf. https://atcoder.jp/contests/abc139/submissions/7298439
  from collections import deque
  
  N = int(input())
  A = [deque(a - 1 for a in map(int, input().split())) for _ in range(N)]
  matchAvailablePlayers = deque(
      range(N)
  )  # 対戦可能な状態にある選手リスト 最初は全選手が対戦可能状態
  daysPassedWhenPlayedMostRecentMatch = [0] * N
  desiredOpponentIdx = [-1] * N  # i番目の選手が次に対戦を希望する相手の番号が[i]に入る
  
  while matchAvailablePlayers:
      playerIdx = matchAvailablePlayers.popleft()
      matchesOrderForThisPlayer = A[playerIdx]  # 鏡像コピー！
  
      if not matchesOrderForThisPlayer:  # この選手はもう全ての相手と対戦した
          continue
  
      opponentIdx = (
          matchesOrderForThisPlayer.popleft()
      )  # 現時点で一番最後に対戦したい相手のindex
  
      if desiredOpponentIdx[opponentIdx] == playerIdx:  # 対戦成立
          daysPassedWhenPlayedMostRecentMatch[playerIdx] = (
              daysPassedWhenPlayedMostRecentMatch[opponentIdx]
          ) = (
              max(
                  daysPassedWhenPlayedMostRecentMatch[playerIdx],
                  daysPassedWhenPlayedMostRecentMatch[opponentIdx],
              )
              + 1
          )  # 互いの「試合に至った日」をどちらか遅い方に合わせその翌日である今日に書き換える
          matchAvailablePlayers.append(playerIdx)  # 試合終了、対戦可能リストに加える
          matchAvailablePlayers.append(opponentIdx)
      else:  # 相手が自分との対戦を望んでいないので対戦できない、このプレイヤーは対戦待ちをする
          desiredOpponentIdx[playerIdx] = opponentIdx
  
  if any(
      A
  ):  # リストのリストなので、if A:だと空リストが中に存在しているだけでもTrueになってしまう
      print(-1)
  else:
      print(max(daysPassedWhenPlayedMostRecentMatch))
  
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."}]