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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC139.E();
        }


        class ABC139
        {
            public static void A()
            {
                var S = Read.Str();
                var T = Read.Str();
                Console.WriteLine(Enumerable.Range(0, 3).Count(i => S[i] == T[i]));
            }
            public static void B()
            {
                var A = Read.Int();
                var B = Read.Int();
                Console.WriteLine((B - 1 + A - 2) / (A - 1));
            }
            public static void C()
            {
                var N = Read.Int();
                var H = Read.IntN();
                int max = 0;
                int current = 0;
                for(int i = 0; i < N - 1; ++i)
                {
                    if (H[i] >= H[i + 1])
                    {
                        current++;
                        max = Math.Max(max, current);
                    }
                    else
                    {
                        current = 0;
                    }
                }
                Console.WriteLine(max);
            }
            public static void D()
            {
                var N = Read.Long();
                Console.WriteLine(N * (N - 1) / 2);
            }
            public static void E()
            {
                var N = Read.Int();
                Func<int, int, int> key = (i, j) => i > j ? j * N + i : i * N + j;
                Dictionary<int, HashSet<int>> Adj = new Dictionary<int, HashSet<int>>();
                var degreeIn = new int[N * N];
                for(int i = 0; i < N; ++i)
                {
                    for(int j = i + 1; j < N; ++j)
                    {
                        var k = key(i, j);
                        Adj.Add(k, new HashSet<int>());
                    }
                }
                for(int i = 0; i < N; ++i)
                {
                    var A = Read.IntN();
                    for(int k = 0; k < N - 2; ++k)
                    {
                        var k1 = key(i, A[k] - 1);
                        var k2 = key(i, A[k + 1] - 1);
                        Adj[k1].Add(k2);
                        degreeIn[k2]++;
                    }
                }
                int start = -1;
                for (int i = 0; i < N; ++i)
                {
                    if (start >= 0) { break; }
                    for (int j = i + 1; j < N; ++j)
                    {
                        var k = key(i, j);
                        if (degreeIn[k] == 0)
                        {
                            start = k;
                            break;
                        }
                    }
                }
                if (start < 0) { Console.WriteLine(-1); return; }
                var done1 = new bool[N * N];
                var done2 = new bool[N * N];
                var history = new Stack<int>();
                var top = Util.TopologicalSort(Adj, start, done1, done2, history);
                if (!top) { Console.WriteLine(-1); return; }
                var dp = new int[N * N];
                while (history.Count > 0)
                {
                    var v = history.Pop();
                    foreach(var w in Adj[v])
                    {
                        dp[w] = Math.Max(dp[w], dp[v] + 1);
                    }
                }
                Console.WriteLine(dp.Max() + 1);
            }
            public static void F()
            {

            }
        }

        class Util
        {
            public static bool TopologicalSort2(Dictionary<int, HashSet<int>> adj, int v, bool[] done1, bool[] done2, Stack<int> history)
            {
                done1[v] = true;
                foreach(var w in adj[v])
                {
                    if (done2[w]) { continue; }
                    if (done1[w] && !done2[w])
                    {
                        return false;
                    }
                    if (!TopologicalSort2(adj, w, done1, done2, history)) { return false; }
                }
                history.Push(v);
                done2[v] = true;
                return true;
            }
            public static bool TopologicalSort(Dictionary<int, HashSet<int>> adj, int start, bool[] done1, bool[] done2, Stack<int> history)
            {
                var stack = new Stack<Tuple<int, bool>>();
                stack.Push(new Tuple<int, bool>(start, false));
                while (stack.Count > 0)
                {
                    var t = stack.Pop();
                    var v = t.Item1;
                    if (t.Item2)
                    {
                        history.Push(v);
                        done2[v] = true;
                        continue;
                    }
                    done1[v] = true;
                    stack.Push(new Tuple<int, bool>(v, true));
                    foreach (var w in adj[v])
                    {
                        if (done2[w]) { continue; }
                        if (done1[w] && !done2[w])
                        {
                            return false;
                        }
                        stack.Push(new Tuple<int, bool>(w, false));
                    }
                }
                return true;
            }
            static bool LoopDetect(Dictionary<int, HashSet<int>> adj, int v, bool[] done1, bool[] done2, Stack<int> history)
            {
                done1[v] = true;
                history.Push(v);
                foreach (var w in adj[v])
                {
                    if (done2[w]) { continue; }
                    if (done1[w] && !done2[w])
                    {
                        history.Push(w);
                        return true;
                    }
                    if (LoopDetect(adj, w, done1, done2, history))
                    {
                        return true;
                    }
                }
                history.Pop();
                done2[v] = true;
                return false;
            }
        }

        static class Read
        {
            static Queue<string> queue = new Queue<string>();
            static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
            public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
            public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
            public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
            public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
            public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
            public static string[] StrN() { return Console.ReadLine().Split(' '); }
            public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
            public static long[,] LongMatrix(int r, int c)
            {
                var mat = new long[r, c];
                for (int i = 0; i < r; ++i)
                {
                    var x = Read.LongN();
                    for (int j = 0; j < c; ++j)
                    {
                        mat[i, j] = x[j];
                    }
                }
                return mat;
            }
            public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M, bool isDirectional = false)
            {
                Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
                for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN(); dict[ab[0]].Add(ab[1]);
                    if (!isDirectional) { dict[ab[1]].Add(ab[0]); }
                }
                return dict;
            }
            public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
            {
                Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new Dictionary<int, long>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1], ab[2]);
                    dict[ab[1]].Add(ab[0], ab[2]);
                }
                return dict;
            }
        }
    }
}