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

days = 0
curarr = arr.map { |a| a.shift }
loop do
  break if curarr.all?(&:nil?)
  match = [*(0..n-1)].select { |i|
    curarr[i] && curarr[curarr[i] - 1] == i + 1
  }
  if match.empty?
    days = -1
    break
  end
  days += 1
  match.each do |i|
    curarr[i] = arr[i].shift
  end
end

puts days