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

Examples

Input

3
2 3
1 3
1 2


Output

3


Input

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


Output

4


Input

3
2 3
3 1
1 2


Output

-1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main 
{
	static class Node
	{
		int a, b;
		boolean rooty = true;
		HashSet<Node> e = new HashSet<>();
		int cache=-100;
		
		public Node(int a0, int b0)
		{
			a = a0;
			b = b0;
		}
		
		int visrun = -1;
	}

	static int n;
	static Node[][] nd;
	static int run = 0;
	static int abortrun = -1;
	
	static int maxh(Node pos)
	{
//		System.out.println("Traverse " + pos.a + " " + pos.b);
		if(pos.cache >= 0)return pos.cache;
		
//		System.out.println("go to " + pos.a + " " + pos.b);
		if(abortrun >= run)return -1;
		pos.visrun = run;
		int ma = 1;
		for(Node e : pos.e)
		{
			if(e.visrun == run)
			{
				abortrun = run;
				return -1;
			}
			ma = Math.max(ma, 1 + maxh(e));
		}
		pos.cache = ma;
		pos.visrun = -1;
		return ma;
	}
	
	public static void main(String[] args) 
	{
		FastReader sc = new FastReader();
		n = sc.nextInt();
		nd = new Node[n][n];
		for(int i = 0; i < n; i++)
			for(int j = i+1; j < n; j++)
			{
				nd[i][j] = new Node(i,j);
			}
		
		for(int i = 0; i < n; i++)
		{
			Node prev = null;
			for(int j = 0; j < n-1; j++)
			{
				int b0 = sc.nextInt()-1;
				int a = Math.min(i, b0);
				int b = Math.max(i, b0);
				Node curn = nd[a][b];
				
				if(prev != null)
				{
					prev.e.add(curn);
					curn.rooty = false;
				}
				prev = curn;
			}
		}
		
		HashSet<Node> roots = new HashSet<>();
		for(int i = 0; i < n; i++)
			for(int j = i+1; j < n; j++)
			{
				if(nd[i][j].rooty)roots.add(nd[i][j]);
			}
		
		if(roots.size() <= 0)
		{
			System.out.println(-1);
			return;
		}
		
		int max = 0;
		for(Node r : roots)
		{
//			System.out.println("start from " + r.a + " " + r.b);
			run++;
			max = Math.max(max, maxh(r));
			if(abortrun >= run)
			{
//				System.out.println("CYC");
				System.out.println(-1);
				return;
			}
		}
		System.out.println(max);
		
	}

    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    } 	
}
