[<AutoOpen>]
module Program

open System

let read f = stdin.ReadLine() |> f
let reada f = stdin.ReadLine().Split() |> Array.map f
let reads f = stdin.ReadLine().Split() |> Array.toList |> List.map f

let solve n xss =
    let ds = Array.create n false
    let clear () = for i = 0 to n - 1 do ds.[i] <- false
    let rec loop acc (xss : int list []) =
        let mutable lock = true
        clear ()
        for i = 0 to n - 1 do
            if not ds.[i] then
                match xss.[i] with
                | [] -> ()
                | y :: ys ->
                    if not ds.[y-1] then
                        match xss.[y-1] with
                        | [] -> ()
                        | z :: zs ->
                            if i = z - 1 then
                                lock <- false
                                ds.[i] <- true
                                ds.[y - 1] <- true
                                xss.[i] <- ys
                                xss.[y - 1] <- zs
        if lock then
            if Seq.exists (List.isEmpty >> not) xss then
                -1
            else
                acc
        else
            loop (acc + 1) xss
    loop 0 xss

[<EntryPoint>]
let main _ =
    let n = read int
    let xss = Array.init n (fun _ -> reads int)
    solve n xss
    |> printfn "%i"
    0