n = gets.to_i
a = []
n.times do |i|
  a.push(gets.split(" ").map(&:to_i))
end

days = 0
while
  days += 1
  drop_list = []
  a.each_with_index do |p, i|
    next if p.empty? || a[p.first - 1].empty?
    if a[p.first - 1].first - 1 == i
      drop_list.push(p.first)
    end
  end
  if drop_list.empty?
    days = -1
    break
  else
    drop_list.each do |p|
      a[p - 1].shift
    end
  end
  break if a.flatten.empty?
end

puts days
