n = gets.chomp.to_i
as = []
n.times do |_|
  as << gets.chomp.split(" ").push("0").map { |num| num.to_i - 1 }
end

loop_count = 0

loop do
  delete_list = []
  n.times do |i|
    if as[as[i][0]][0].eql?(i)
      delete_list << i
      #delete_list << as[i][0]
    end
  end

  break if delete_list.empty?

  loop_count += 1
  delete_list.each do |delete_index|
    as[delete_index].shift
  end
end

as.each do |a|
  unless a[0].eql?(-1)
    puts -1
    exit
  end
end

puts loop_count
