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.util.Map.Entry;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
 
public class Main //implements Runnable
{
    static long mod=1000000007;
//    static int dx[]={-1,0,1,0};
//    static int dy[]={0,-1,0,1};

    public static void main(String[] args)   throws Exception, IOException{        
        Reader sc = new Reader(System.in);
        PrintWriter out=new PrintWriter(System.out);

        int n=sc.nextInt();
//        ArrayList<Integer> l[]=new ArrayList[n];
        int d[][]=new int[n][n];
        int cur[]=new int[n];
        for (int i = 0; i < d.length; i++) {
			for (int j = 0; j < d.length-1; j++) {
				d[i][j]=sc.nextInt()-1;
			}
		}
        boolean u[]=new boolean[n];
        boolean fin[]=new boolean[n];
        int a=0;
    	int c=0,v=0;
    	
    	ArrayList<Integer> l=new ArrayList<>();

    	lp:
        while(true) {
        	fill(u,false);
        	a++;
        	v=c;
        	if(a==1)
        	for (int i = 0,t; i < d.length; i++) {
				if(fin[i]) {continue;}
        		t=d[i][cur[i]];
				if(u[t]||u[i])continue;
				if(fin[t]) {a=-1;break lp;}
				if(i==d[t][cur[t]]) {
					cur[i]++;
					cur[t]++;
					u[i]=true;
					u[t]=true;
					c+=2;
//					db(i,t,a);
					if(cur[i]==cur.length-1) {fin[i]=true;}
					if(cur[t]==cur.length-1) {fin[t]=true;}
				}
			}
        	else
        	for (int i = 0,t,w=0; w < l.size(); w++) {
        		i=l.get(w);
//				db(i);
        		if(fin[i]) {continue;}
        		t=d[i][cur[i]];
				if(u[t]||u[i])continue;
				if(fin[t]) {a=-1;break lp;}
				if(i==d[t][cur[t]]) {
					cur[i]++;
					cur[t]++;
					u[i]=true;
					u[t]=true;
					c+=2;
//					db(i,t,a);
					if(cur[i]==cur.length-1) {fin[i]=true;}
					if(cur[t]==cur.length-1) {fin[t]=true;}
				}
			}
        	l.clear();

        	for (int i = 0; i < u.length; i++) {
				if(!u[i])l.add(i);
			}
        	
//        	db(c,v);
			if(c==n*(n-1))break;
        	if(v==c) {
        		a=-1;break;
        	}
		}
        
        out.println(a);
     	out.flush();
    }
    
    static boolean validpos(int x,int y,int r, int c){
        return x<r && 0<=x && y<c && 0<=y;
    }

    static void db(Object... os){
        System.err.println(Arrays.deepToString(os));
    }
}

//class P {
//	int x, y;
//	P(int  x, int y) {
//	  this.x=x;
//	  this.y=y;
//	}
//}

//class P implements Comparable<P>{
//    int a,b;
//    P(int a, int b) {
//    	this.b=b;
//    	this.a=a;
//    }
//
//    public int compareTo(P p){
//    	if(a==p.a) return b-p.b;
//        return -p.a+a;
//    }
//}

class Reader
{ 
    private BufferedReader x;
    private StringTokenizer st;
    
    public Reader(InputStream in)
    {
        x = new BufferedReader(new InputStreamReader(in));
        st = null;
    }
    public String nextString() throws IOException
    {
        while( st==null || !st.hasMoreTokens() )
            st = new StringTokenizer(x.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException
    {
        return Integer.parseInt(nextString());
    }
    public int[] nextIntArray(int size) throws IOException{
        int r[] = new int[size];
        for (int i = 0; i < size; i++) {
            r[i] = this.nextInt(); 
        }
        return r;
    }
    public long[] nextLongArray(int size) throws IOException{
        long r[] = new long[size];
        for (int i = 0; i < size; i++) {
            r[i] = this.nextLong(); 
        }
        return r;
    }
    public long nextLong() throws IOException
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}