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 Go (1.6) (judge verdict: Time Limit Exceeded)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Match struct {
	P1 int
	P2 int
}

func makeMatch(p1, p2 int) Match {
	if p1 > p2 {
		p1, p2 = p2, p1
	}
	return Match{
		P1: p1,
		P2: p2,
	}
}

func main() {
	var n int
	fmt.Scan(&n)

	var sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	to := make(map[Match][]Match)
	from := make(map[Match]map[Match]bool)
	for i := 0; i < n; i++ {
		pa := -1
		for j := 0; j < n-1; j++ {
			sc.Scan()
			a, _ := strconv.Atoi(sc.Text())
			a--
			if pa >= 0 {
				// i-1:pa => i-1:a
				pm := makeMatch(i, pa)
				cm := makeMatch(i, a)
				if _, ok := to[pm]; !ok {
					to[pm] = make([]Match, 0)
				}
				to[pm] = append(to[pm], cm)
				if _, ok := from[cm]; !ok {
					from[cm] = make(map[Match]bool)
				}
				from[cm][pm] = true
			}
			pa = a
		}
	}

	queue := make([]Match, 0)
	for i := 0; i < n; i++ {
		for j := 0; j < i; j++ {
			m := makeMatch(i, j)
			if _, ok := from[m]; !ok {
				queue = append(queue, m)
			}
		}
	}

	// proc
	var day int
	var cnt int
	for len(queue) > 0 {
		day++
		tp := make([]bool, n)
		nq := make([]Match, 0)
		for _, m := range queue {
			if !tp[m.P1] && !tp[m.P2] {
				tp[m.P1] = true
				tp[m.P2] = true
				for _, cm := range to[m] {
					var l int
					for fm := range from[cm] {
						if fm == m {
							from[cm][fm] = false
						}
						if from[cm][fm] {
							l++
						}
					}
					if l == 0 {
						nq = append(nq, cm)
					}
				}
				cnt++
			} else {
				nq = append(nq, m)
			}
		}
		queue = nq
	}

	var ok = cnt == n*(n-1)/2

	if ok {
		fmt.Println(day)
	} else {
		fmt.Println(-1)
	}
}