module IntPairSet = Set.Make (struct
  type t = int * int
  let compare = compare
end)

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.(i).(ass.(i).(j)) <-
        (i, ass.(i).(j + 1)) ::
        (ass.(i).(j + 1), i) ::
        es.(i).(ass.(i).(j));
      es.(ass.(i).(j)).(i) <-
        (i, ass.(i).(j + 1)) ::
        (ass.(i).(j + 1), i) :: es.(ass.(i).(j)).(i);
    done
  done;
  let dp = Array.make_matrix n n (-1) in
  let rec visit prev visited i j =
    if IntPairSet.mem (i, j) visited
    then raise Not_found
    else if dp.(i).(j) <= prev
    then begin
      dp.(i).(j) <- 1 + prev;
      let visited' = IntPairSet.add (i, j) visited in
      List.iter (fun (k, l) -> visit dp.(i).(j) visited' k l) es.(i).(j)
    end in
  try
    for i = 0 to n - 1 do
      visit 0 IntPairSet.empty i ass.(i).(0)
    done;
    Printf.printf "%d\n" @@
    Array.fold_left (Array.fold_left max) min_int dp
  with Not_found -> print_endline "-1"




  
