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 Rust (1.15.1) (judge verdict: Accepted)

use std::cmp::max;

fn solve(aa: Vec<Vec<usize>>) -> isize {
    let mut aa = aa;
    let n = aa[0].len();
    aa.insert(0, vec![0; n]);

    let mut days = vec![1; aa.len()];
    let mut idxs = vec![0; aa.len()];
    idxs[0] = aa[0].len();

    loop {
        let mut flags = vec![false; aa.len()];
        for i in 1..aa.len() {
            let id1 = idxs[i];
            if id1 >= aa[i].len() {
                continue;
            }
            let t = aa[i][id1];
            let id2 = idxs[t];
            if id2 >= aa[i].len() {
                continue;
            }
            let tt = aa[t][id2];

            if tt == i {
                idxs[i] += 1;
                idxs[t] += 1;
                days[i] += 1;
                days[t] += 1;
                days[i] = max(days[i], days[t]);
                days[t] = max(days[i], days[t]);
                flags[i] = true;
                flags[t] = true;
            }
        }
        if flags.iter().all(|&f| f == false) {
            if idxs.iter().all(|&id| id == aa[0].len()) {
                return *days.iter().max().unwrap() as isize - 1;
            } else {
                return -1;
            }
        }
    }
}

#[test]
fn solve_test() {
    assert_eq!(solve(vec![vec![2, 3], vec![1, 3], vec![1, 2]]), 3);
    assert_eq!(
        solve(vec![
            vec![2, 3, 4],
            vec![1, 3, 4],
            vec![4, 1, 2],
            vec![3, 1, 2]
        ]),
        4
    );
    assert_eq!(solve(vec![vec![2, 3], vec![3, 1], vec![1, 2]]), -1);
}

#[allow(unused_macros)]
macro_rules! parse_line {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            iter.next().unwrap().parse::<$t>().unwrap()
        }
    );

    ( $( $t:ty), +) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ( $(iter.next().unwrap().parse::<$t>().unwrap()),* )
        }
    );
}

#[allow(unused_macros)]
macro_rules! read_line {
    () => {{
        let mut line = String::new();
        ::std::io::stdin().read_line(&mut line).unwrap();
        line.pop();
        line
    }};
}

#[allow(unused_macros)]
macro_rules! parse_vec {
    ( $t:ty ) => {{
        let mut line = String::new();
        ::std::io::stdin().read_line(&mut line).unwrap();
        let iter = line.split_whitespace();
        iter.map(|v| v.parse::<$t>().unwrap()).collect::<Vec<_>>()
    }};
}

fn main() {
    let n = parse_line!(usize);
    let aa = (0..n).map(|_| parse_vec!(usize)).collect::<Vec<_>>();

    println!("{}", solve(aa));
}