Scanf.scanf "%d" (fun n ->
    let sz = n * (n - 1) / 2 in
    let nei = Array.make sz [] in
    let count = Array.make sz 0 in
    let rec game a b = if a < b then game b a else a * (a - 1) / 2 + b in

    let arr = Array.init n (fun _ ->
        Array.init (n - 1) (fun _ -> Scanf.scanf " %d" (fun a -> a - 1))
    )
    in
    for i = 0 to n - 1 do
        for j = 0 to n - 3 do
            let g1 = game i arr.(i).(j) in
            let g2 = game i arr.(i).(j + 1) in
            nei.(g1) <- g2 :: nei.(g1);
            count.(g2) <- count.(g2) + 1
        done
    done;

    Printf.printf "%d\n" @@
    let days = Array.make sz (-1) in
    try
        for i = 0 to sz - 1 do
            let rec dfs k =
                if days.(k) > 0 then days.(k) else
                    if days.(k) = 0 then failwith "loop" else (
                        days.(k) <- 0;
                        let r = List.fold_left (fun acc v -> max acc (dfs v)) 0 nei.(k) in
                        days.(k) <- r + 1;
                        r + 1
                    )
            in
            if count.(i) = 0 then ignore (dfs i)
        done;
        Array.fold_left max (-1) days
    with _ -> -1
)