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# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Math;

class MainClass : Scanner
{
    static void Main()
    {
        int N = RInt();
        var G = new Graph<int>(N*N);
        var G1 = new Graph<int>(N * N);
        var A = new int[N][];
        int cnt = 0;
        for (int me = 0; me < N; me++)
        {
            A[me] = RInts(_=>int.Parse(_)-1);

            int from = -1;

            foreach (var en in A[me])
            {
                if (from == -1)
                {
                    from = GetIndex(me,en,N);continue;
                }
                var to = GetIndex(me,en, N);
                G.Add(from, to);
                G1.Add(to, from);
                from = to;
            }

        }
        
        var sp = new List<int>();
        var degree = new int[N * N ];
        var sorted = new List<int>();
        for (int i = 0; i < N*N; i++)
        {
            degree[i] = G1[i].Count;
            
        }
        sorted =  TopologicalSort.Topologicalsort(G, degree);

        
        if (G[sorted[1]].Count == 0)
        {
            WriteLine(-1);
            return;
        }
        {
            int i = sorted[1];
            var days = new int[N * N];
            var que = new Stack<(int, int)>();
            que.Push((i, 1));
            days[i] = 1;
            while (que.Count > 0)
            {
                var v = que.Pop();
                int now = v.Item1;
                int day = v.Item2;
                if (day > N * N )
                {
                    WriteLine(-1);
                    return;
                }
                foreach (var j in G[now])
                {
                    var nday = day + 1;
                    if (days[j] < nday)
                    {
                        days[j] = nday;
                        que.Push((j, nday));
                    }
                }
            }
            WriteLine(days.Max());
        }
        
    }
    static int GetIndex(int a,int b,int n)
    {
        if (a < b) return a * n + b;
        else return b * n + a;
    }

}
class Graph<T>
{
    private HashSet<T>[] G;
    public Graph(int size)
    {
        G = new HashSet<T>[size];
        for (int i = 0; i < size; i++)
        {
            G[i] = new HashSet<T>();
        }
    }

    public HashSet<T> this[int i] => G[i];
    public void Add(int from, T to)
    {
        if(!G[from].Contains(to)) G[from].Add(to);
    }
    public HashSet<T>[] ToListArray() => G;
    public int Length => G.Length;
}
static class TopologicalSort
{
    //Degreesはその頂点からのびる辺の数
    public static List<int> Topologicalsort(Graph<int> G, int[] Degrees)
    {
        var que = new Queue<int>();
        for (int i = 0; i < Degrees.Length; i++)
        {
            if (Degrees[i] == 0)
                que.Enqueue(i);
        }

        var sorted = new List<int>();
        while (que.Count > 0)
        {
            var v = que.Dequeue();
            sorted.Add(v);
            foreach (var i in G[v])
            {
                Degrees[i]--;
                if (Degrees[i] == 0)
                    que.Enqueue(i);
            }
        }
        return sorted;
    }
}
class Scanner
{
    public static int RInt() => int.Parse(Console.ReadLine());
    public static long RLong() => long.Parse(Console.ReadLine());
    public static string RString() => Console.ReadLine();
    public static double RDouble() => double.Parse(Console.ReadLine());

    public static int[] RInts() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
    public static int[] RInts(Func<string, int> func) => Console.ReadLine().Split().Select(func).ToArray();
    public static long[] RLongs() => Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
    public static long[] RLongs(Func<string, long> func) => Console.ReadLine().Split().Select(func).ToArray();
    public static double[] RDoubles() => Array.ConvertAll(Console.ReadLine().Split(), double.Parse);
    public static double[] RDoubles(Func<string, double> func) => Console.ReadLine().Split().Select(func).ToArray();
    public static string[] RStrings() => Console.ReadLine().Split();
    //public static string[] RStrings(Func<string, string> func) => Console.ReadLine().Split().Select(func).ToArray();
    public static T1 ReadStream<T1>()
    {
        var r = RStrings();
        var r1 = (T1)Convert.ChangeType(r[0], typeof(T1));
        return r1;
    }
    public static (T1, T2) ReadStream<T1, T2>()
    {
        var r = RStrings();
        var r1 = (T1)Convert.ChangeType(r[0], typeof(T1));
        var r2 = (T2)Convert.ChangeType(r[1], typeof(T2));
        return (r1, r2);
    }
    public static (T1, T2, T3) ReadStream<T1, T2, T3>()
    {
        var r = RStrings();
        var r1 = (T1)Convert.ChangeType(r[0], typeof(T1));
        var r2 = (T2)Convert.ChangeType(r[1], typeof(T2));
        var r3 = (T3)Convert.ChangeType(r[2], typeof(T3));
        return (r1, r2, r3);
    }
    public static (T1, T2, T3, T4) ReadStream<T1, T2, T3, T4>()
    {
        var r = RStrings();
        var r1 = (T1)Convert.ChangeType(r[0], typeof(T1));
        var r2 = (T2)Convert.ChangeType(r[1], typeof(T2));
        var r3 = (T3)Convert.ChangeType(r[2], typeof(T3));
        var r4 = (T4)Convert.ChangeType(r[3], typeof(T4));
        return (r1, r2, r3, r4);
    }
}