n = gets.to_i

a = n.times.map do |_|
  gets.split.map(&:to_i)
end

i = 1
while true do
  match = Hash.new
  is_empty = true
  n.times do |j|
    b = a[j].first.to_i
    if match[j].nil? && match[b-1].nil? && j == a[b-1].first.to_i - 1
      a[j].shift
      a[b-1].shift
      match[j] = true
      match[b-1] = true
    end
    if !a[j].empty?
      is_empty = false
    end
  end

  if match.length == 0
    p -1
    exit
  end

  if is_empty
    p i
    exit
  end

  i += 1
end