Problem:
Score : 500 points

Problem 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.

Constraints

3 \leq N \leq 1000

1 \leq A_{i, j} \leq N

A_{i, j} \neq i

A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

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}

Output

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.

Sample Input 1

3
2 3
1 3
1 2

Sample Output 1

3

All the conditions can be satisfied if the matches are scheduled for three days as follows:

Day 1: Player 1 vs Player 2

Day 2: Player 1 vs Player 3

Day 3: Player 2 vs Player 3

This is the minimum number of days required.

Sample Input 2

4
2 3 4
1 3 4
4 1 2
3 1 2

Sample Output 2

4

All the conditions can be satisfied if the matches are scheduled for four days as follows:

Day 1: Player 1 vs Player 2, Player 3 vs Player 4

Day 2: Player 1 vs Player 3

Day 3: Player 1 vs Player 4, Player 2 vs Player 3

Day 4: Player 2 vs Player 4

This is the minimum number of days required.

Sample Input 3

3
2 3
3 1
1 2

Sample Output 3

-1

Any scheduling of the matches violates some condition.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

//試合を頂点とするグラフに変換して解く方法

import java.util.*;
import java.io.*;
public class Main {
	static int id[][];
	static List<ArrayList<Integer>> to; //試合のノードとするグラフ 
	static int dp[];
	static List<Integer> start;
	static int matchId;
	static boolean infLoop;
	public static void main(String[] args) {
		FastScanner sc = new FastScanner(System.in);
		int N = sc.nextInt();
		int A[][] = new int[N][N-1];
		for(int i=0;i<N;i++) {
			for(int j=0;j<N-1;j++) {
				A[i][j]=sc.nextInt()-1;
			}
		}
		
		//試合にIdを振っていく
		id = new int[N][N];
		for(int i=0;i<N;i++)Arrays.fill(id[i], -1); //−１で初期化
		matchId = 0; //頂点番号になる
		//試合に順番にIDを降っていくと、1-4, 4-1などでダブルカウントが発生するので、i<jの時(例なら1-4)の時だけIDをふる。
		for(int i=0;i<N;i++) for(int j=0;j<N;j++) {
			if(i<j)id[i][j] = matchId++;
		}
		
		//連結行列のtoを作っていく
		int M = N*(N-1)/2;
		to = new ArrayList<>(M);
		
		//TLE対策（recを全頂点から回すとTLEするので）
		//int notStart[] = new int[matchId];
		
		for(int i=0;i<M;i++)to.add(new ArrayList<Integer>());
		for(int i=0;i<N;i++)for(int j=0;j<N-2;j++) {
			int player1 = i;
			int player2 = A[i][j];
			int next_player2 = A[i][j+1];
			int id1 = toId(player1, player2);
			int id2 = toId(player1, next_player2);
			//notStart[id2]=1;
			to.get(id1).add(id2);
		}
		
//		start = new ArrayList<>();
//		for(int i=0;i<matchId;i++) {
//			if(notStart[i]==1)continue;
//			start.add(i);
//		}
	
		//メモ化再帰で探索する。
		dp = new int[M]; //ある試合から最後の試合までの日数（グラフでいう距離）
		Arrays.fill(dp, -1);
		long ans = 0;
		
		infLoop = false;
		//for(int i:start) ans = Math.max(ans,rec(i));
		for(int i=0;i<matchId;i++) ans = Math.max(ans,rec(i,0));
		if(infLoop) {
			System.out.println(-1);
			return;
		}
		
		if(ans == 0) {
			System.out.println(-1);
			return;
		}
		System.out.println(ans);
	}
	//対戦相手２人を入力にして、試合IDを返すメソッド
	static int toId(int x, int y) {
		if(x > y) return toId(y, x);
		return id[x][y];
	}
	
	static int rec(int v, int depth) {
		if(depth>matchId) {
				if(dp[v]==-1) {
				infLoop = true;
				System.out.println(-1);
				return -1;
			}
		}
		if(dp[v]!=-1)return dp[v];
		int ret = 1;
		for(int next:to.get(v)) {
			ret = Math.max(ret, rec(next, depth+1)+1);
		}

		return dp[v] = ret;
	}
	
}



class FastScanner {
  private BufferedReader reader = null;
  private StringTokenizer tokenizer = null;

  public FastScanner(InputStream in) {
      reader = new BufferedReader(new InputStreamReader(in));
      tokenizer = null;
  }

  public String next() {
      if (tokenizer == null || !tokenizer.hasMoreTokens()) {
          try {
              tokenizer = new StringTokenizer(reader.readLine());
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }
      return tokenizer.nextToken();
  }

  public String nextLine() {
      if (tokenizer == null || !tokenizer.hasMoreTokens()) {
          try {
              return reader.readLine();
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }

      return tokenizer.nextToken("\n");
  }

  public long nextLong() {
      return Long.parseLong(next());
  }

  public int nextInt() {
      return Integer.parseInt(next());
  }

  public double nextDouble() {
       return Double.parseDouble(next());
   }

  public int[] nextIntArray(int n) {
      int[] a = new int[n];
      for (int i = 0; i < n; i++)
          a[i] = nextInt();
      return a;
  }

  public long[] nextLongArray(int n) {
      long[] a = new long[n];
      for (int i = 0; i < n; i++)
          a[i] = nextLong();
      return a;
  } 
}