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