open Scanf;;
open Printf;;

let rint () = scanf " %d" (fun x -> x);;

let () =
  let n = rint () in
  let index i j =
    let i' = min i j in
    let j' = max i j in
    i' * n + j' in
  let g = Array.make (n * n) [] in
  let deg = Array.make (n * n) 0 in
  for i = 0 to n - 1 do
    let a = Array.init (n - 1) (fun _ -> rint () - 1) in
    for j = 0 to n - 3 do
      let u = index i a.(j) in
      let v = index i a.(j + 1) in
      g.(u) <- v :: g.(u);
      deg.(v) <- deg.(v) + 1;
    done;
  done;
  let q = Queue.create () in
  for i = 0 to n * n - 1 do
    if deg.(i) = 0 then
      Queue.add i q;
  done;
  let dp = Array.make (n * n) 0 in
  while not (Queue.is_empty q) do
    let u = Queue.pop q in
    g.(u) |> List.iter @@ fun v ->
                          dp.(v) <- max dp.(v) (dp.(u) + 1);
                          deg.(v) <- deg.(v) - 1;
                          if deg.(v) = 0 then
                            Queue.add v q;
  done;
  if Array.exists ((<) 0) deg then
    print_endline "-1"
  else
    printf "%d\n" ((Array.fold_left max min_int dp) + 1);
