let () = Scanf.scanf "%d\n" @@ fun n ->
  let ass =
    Array.init n @@ fun _ ->
      Array.init (n - 1) @@ fun _ ->
        Scanf.scanf "%d " @@ fun a -> a - 1 in
  let es = Array.make_matrix n n [] in
  for i = 0 to n - 1 do
    for j = 0 to n - 3 do
      es.(min i ass.(i).(j)).(max i ass.(i).(j)) <-
        (min i ass.(i).(j + 1), max i ass.(i).(j + 1)) ::
        es.(min i ass.(i).(j)).(max i ass.(i).(j))
    done
  done;
  let dp = Array.make_matrix n n (-1) in
  let vs = Array.make_matrix n n false in
  let rec visit acc (i, j) =
    if vs.(i).(j)
    then raise Not_found
    else begin
      vs.(i).(j) <- true;
      let ans = List.fold_left visit ((i, j) :: acc) es.(i).(j) in
      vs.(i).(j) <- false;
      ans
    end in
  try
    let sort =
      Array.fold_left visit [] @@
      Array.init n @@ fun i ->
        (min i ass.(i).(0), max i ass.(i).(0)) in
    let rec update prev (i, j) =
      if dp.(i).(j) <= prev
      then begin
        dp.(i).(j) <- prev + 1;
        List.iter (update dp.(i).(j)) es.(i).(j)
      end in
    List.iter (update 0) sort;
    Printf.printf "%d\n" @@
    Array.fold_left (Array.fold_left max) min_int dp
  with Not_found -> print_endline "-1"

