n = gets.chomp.to_i
a = []
n.times do |i|
  a[i] = gets.chomp.split(" ").map{|tmp| tmp.to_i - 1}
end

days = 0

loop do
  days += 1
  current_drop_indexes = {}
  a.each_with_index do |vals, i|
    next if current_drop_indexes[i] == 1
    next if vals.size == 0
    target = vals.first
    if a[target].first && a[target].first == i
      # current_drop_indexes.concat([i, target])
      current_drop_indexes[i] = 1
      current_drop_indexes[target] = 1
    end
  end
  keys = current_drop_indexes.keys
  if keys.size == 0
    # failed
    puts "-1"
    exit
  end
  keys.each do |i|
    a[i].shift
  end
  if a.all?{|vals| vals.first.nil?}
    # success
    puts days
    exit
  end
end
