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 i j =
    if vs.(i).(j)
    then raise Not_found
    else if 0 <= dp.(i).(j) then dp.(i).(j)
    else begin
      vs.(i).(j) <- true;
      dp.(i).(j) <-
        1 + List.fold_left (fun acc (i, j) ->
          max acc @@ visit i j) 0 es.(i).(j);
      vs.(i).(j) <- false;
      dp.(i).(j);
    end in
  try
    Printf.printf "%d\n" @@
    Array.fold_left (Array.fold_left max) min_int @@
    Array.init n @@ fun i -> Array.init n @@ visit i
  with Not_found -> print_endline "-1"

