n = gets.to_i
arr = [*(1..n)].map { |_| gets.chomp.split(' ').map(&:to_i) }

days = 0
loop do
  break if arr.all?(&:empty?)
  match = []
  arr.each.with_index do |a, i|
    next if a.empty?
    if arr[a.first - 1].first == i + 1
      match << i
    end
  end
  if match.empty?
    days = -1
    break
  end
  days += 1
  match.each do |i|
    arr[i].shift
  end
end

puts days