n = gets.chomp.to_i
a = []

n.times {
    line = gets.chomp.split(" ").map(&:to_i)
    a.push(line)
}

day = 0
games = 0

while games < n*(n-1)/2
    pair = 0
    match = Array.new(n,false)
    for i in 1..n do
        if match[i-1] 
            next
        end
        tar = a[i-1].first
        if ! tar.nil? 
            if a[tar-1].first == i && ! match[tar-1]
                match[i-1] = true
                a[i-1].shift
                match[tar-1] = true
                a[tar-1].shift
                pair += 1
                # print i," vs ",tar,"\n"
            end
        end
    end
    if pair == 0
        day = -1
        break
    end
    day += 1
    games += pair
end
p day
