[<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
            match xss.[i] with
            | [] -> ()
            | y :: ys ->
                match xss.[y-1] with
                | [] -> ()
                | z :: zs ->
                    if i = z - 1 && not ds.[i] && not ds.[y - 1] then
                        lock <- false
                        ds.[i] <- true
                        ds.[y - 1] <- true
                        xss.[i] <- ys
                        xss.[y - 1] <- zs
        if lock then
            if Array.forall List.isEmpty xss then
                acc
            else
                -1
        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