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

module M = Map.Make (struct type t = int let compare = compare end)

let () =
    let split s =
        let rec loop i cur flag acc =
            if i = String.length s then ( if flag then cur :: acc else acc) else (
                match flag, s.[i] with
                | true,  ' ' -> loop (i + 1) "" false (cur :: acc)
                | false, ' ' -> loop (i + 1) "" false acc
                | true,  v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
                | false, v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
            )
        in
        List.map int_of_string (List.rev (loop 0 "" false []))
    in
    let main () =
        let n = int_of_string (read_line ()) in
        let a = Array.init n (fun _ -> Array.of_list (split (read_line ()))) in
        let add key v l map =
            l.(v) <- l.(v) + 1;
            M.add key (v :: try M.find key map with _ -> []) map
        in
        let canonical (a, b) = if a < b then a * 1024 + b else b * 1024 + a in
        let l = Array.make (1024 * 1024) 0 in
        let rec loop_i i map =
            let rec loop_j j map =
                if j = n - 1 then map else
                    let v = a.(i).(j) in
                    let k = canonical (i + 1, v) in
                    let map =
                        if j = 0 then (
                            if v > i + 1 then add 0 k l map else map
                        ) else (
                            let vv = a.(i).(j - 1) in
                            add (canonical (i + 1, vv)) k l map
                        )
                    in
                    loop_j (j + 1) map
            in
            if i = n then map else loop_i (i + 1) (loop_j 0 map)
        in
        let map = loop_i 0 (M.singleton 0 []) in
        let rec emit day rest check map next arr = function
            | [] -> if rest = 0 then day else
                    if next <> [] then emit (day + 1) rest check map [] (Array.make (n + 1) false) next
                    else (-1)
            | cd :: tl ->
                    let c = cd lsr 10 in
                    let d = cd land 1023 in
                    if arr.(c) || arr.(d) then emit day rest check map (cd :: next) arr tl else
                        (*
                        let () = Printf.printf "day %d: can emit %d,%d\n" day c d in
        *)
                        let () = arr.(c) <- true in
                        let () = arr.(d) <- true in
                        let next = List.fold_left (fun acc cd ->
                            check.(cd) <- check.(cd) - 1;
                            if check.(cd) = 0 then cd :: acc else acc
                        ) next (try M.find cd map with _ -> [])
                        in
                        emit day (rest - 1) check map next arr tl
        in
        let init = List.fold_left (fun acc cd ->
            l.(cd) <- l.(cd) - 1;
            if l.(cd) = 0 then cd :: acc else acc) [] (M.find 0 map)
        in
        (*
        let () = List.iter (fun (c, d) -> Printf.printf "(%d, %d) " c d) init in
        let () = print_newline () in
    *)
        let r = emit 1 (n * (n - 1) / 2) l map [] (Array.make (n + 1) false) init in
        Printf.printf "%d\n" r
    in
    main ()