N = gets.to_i
A = N.times.map{ gets.split.map(&:to_i) }.unshift([])
ans = 0
while !A.flatten.empty?
    ans += 1
    matched = Array.new(N+1, false)
    A.each_with_index do |a, idx|
        next if a.empty? || A[a[0]].empty?
        if (!matched[idx] && !matched[a[0]] && A[a[0]][0] == idx)
            matched[idx] = true
            matched[a[0]] = true
            A[a[0]].shift
            a.shift
        end
    end
    if !matched.any?{|m| m}
        p -1
        exit
    end
end
p ans