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: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    using System;
    using System.Linq;

    using Match = System.Int32;

    class Program
    {
        static readonly int PLAYER_ID_BASE = 10000;
        static Dictionary<Match, List<Match>> PreMatches = new Dictionary<Match, List<Match>>();
        static Dictionary<Match, List<Match>> PostMatches = new Dictionary<Match, List<Match>>();
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            // スペース区切りの整数の入力
            List<int[]> inputs = new List<int[]>();
            for(int i = 0; i < n; i++)
            {
                inputs.Add(Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray());
            }

            // 循環参照判定用
            Dictionary<Match, bool> reachedMatches = new Dictionary<Match, bool>();

            // 最長パス探索用
            Dictionary<Match, int> depthOfMatches = new Dictionary<Match, int>();

            // PreMatches と PostMatches と循環参照判定用の Dictionary の初期化
            for (int i = 1; i <= n; i++)
            {
                for (int j = i + 1; j <= n; j++)
                {
                    Match tmpMatch = GetMatchId(i, j);
                    PreMatches[tmpMatch] = new List<Match>();
                    PostMatches[tmpMatch] = new List<Match>();
                    reachedMatches[tmpMatch] = false;
                    depthOfMatches[tmpMatch] = -1;
                }
            }

            // 選手 i の j 番目の要求について前後の値を確認し、 pre と post に設定する。
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n - 1; j++)
                {
                    Match thisMatch = GetMatchId(i + 1, inputs[i][j]);
                    if (j > 0)
                    {
                        Match PreMatch = GetMatchId(i + 1, inputs[i][j - 1]);
                        PreMatches[thisMatch].Add(PreMatch);
                    }
                    if (j < n - 2)
                    {
                        Match PostMatch = GetMatchId(i + 1, inputs[i][j + 1]);
                        PostMatches[thisMatch].Add(PostMatch);
                    }
                }
            }

            //// 第一引数は何でも良い？
            //bool isFeasible = IsAcyclicDFSForward(GetMatchId(1, 2), reachedMatches);
            //// reachedMatches を初期化
            //Console.ReadLine();
            //for (int i = 1; i <= n; i++)
            //{
            //    for (int j = i + 1; j <= n; j++)
            //    {
            //        Match tmpMatch = GetMatchId(i, j);
            //        reachedMatches[tmpMatch] = false;
            //    }
            //}
            //isFeasible = isFeasible && IsAcyclicDFSBackward(GetMatchId(1, 2), reachedMatches);

            // トポロジカルソート風
            Dictionary<Match, int> preNumbers = PreMatches.ToDictionary(v => v.Key, v => v.Value.Count());
            List<Match> sources = preNumbers.Where(v => v.Value == 0).Select(v => v.Key).ToList();
            List<Match> sortedMatches = new List<Match>();
            while (sources.Count() > 0)
            {
                // Console.WriteLine("sources : [ " + string.Join(" ", sources.Select(v => v.ToString())) + " ]");
                // Console.WriteLine("sorteds : [ " + string.Join(" ", sortedMatches.Select(v => v.ToString())) + " ]");
                Match topMatch = sources[0];
                sources.RemoveAt(0);
                PostMatches[topMatch].ForEach(v => {
                    preNumbers[v]--;
                    if (preNumbers[v] == 0)
                    {
                        sources.Add(v);
                    }
                    });
                sortedMatches.Add(topMatch);
            }

            // n 頂点が全てトポロジカルソートされたら、条件を満たす日程を組める。
            bool isFeasible = sortedMatches.Count() == (n * (n - 1)) / 2;
            if (!isFeasible)
            {
                Console.WriteLine("-1");
                return;
            }

            // 最長経路の探索（全試合を行うために何日必要かに相当）
            int RequiredDays = 0;
            for(int i = 1; i <= n; i++)
            {
                for(int j = i + 1; j <= n; j++)
                {
                    RequiredDays = Math.Max(RequiredDays, MaxDepthOf(GetMatchId(i, j), depthOfMatches));
                }
            }

            //出力
            Console.WriteLine(RequiredDays.ToString());
            // Console.ReadLine();
        }

        static Match GetMatchId(int player1ID, int player2ID)
        {
            return Math.Min(player1ID, player2ID) * PLAYER_ID_BASE + Math.Max(player1ID, player2ID);
        }

        static bool IsAcyclicDFSForward(Match current, Dictionary<Match, bool> reachedMatches)
        {
            Console.WriteLine(current.ToString() + " : [ " + string.Join(" ", reachedMatches.Values.Select(v => v?"1" : "_")) + " ]");
            if (reachedMatches[current]) return false;
            reachedMatches[current] = true;
            List<Match> nextMatches = PostMatches[current];
            Console.WriteLine(current.ToString() + " -> " + string.Join(" ", nextMatches));
            return nextMatches.Aggregate(true, (result, next) => result && IsAcyclicDFSForward(next, reachedMatches));
        }

        static bool IsAcyclicDFSBackward(Match current, Dictionary<Match, bool> reachedMatches)
        {
            if (reachedMatches[current]) return false;
            reachedMatches[current] = true;
            List<Match> nextMatches = PreMatches[current];
            return nextMatches.Aggregate(true, (result, next) => result && IsAcyclicDFSBackward(next, reachedMatches));
        }

        static int MaxDepthOf(Match target, Dictionary<Match, int> depthOfMatches)
        {
            if (depthOfMatches[target] < 0)
            {
                List<Match> nextMatches = PreMatches[target];
                depthOfMatches[target] = nextMatches.Aggregate(0, (result, next) => Math.Max(result, MaxDepthOf(next, depthOfMatches))) + 1;
            }
            return depthOfMatches[target];
        }
    }

    class MatchOrder
    {
        Match PreMatch { get; set; }
        Match PostMatch { get; set; }
    }
}