N = gets.to_i
VS = Array.new(N + 1)
(1 .. N).each do |i|
  VS[i] = gets.split.map(&:to_i)
end

used = Array.new(N + 1, false)
days = 0

loop do
  found = false
  used.fill(false)
  
  (1 .. N).each do |i|
    next if used[i] || VS[i].empty?
    j = VS[i][0]
    
    if !used[j] && VS[j][0] == i
      used[i] = used[j] = true
      VS[i].shift
      VS[j].shift
      found = true
    end
  end
  if found
    days += 1
  else
    break
  end
end

if (1 .. N).all?{|i| VS[i].empty? }
  puts days
else
  puts -1
end
