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