n = gets.to_i
matches = []
n.times { matches << gets.split.map(&:to_i) }
queue = (1..n).to_a
delete_list = []
day = 0

catch(:break_loop) do
until queue.empty?
  until queue.empty?
    me = queue.shift
    if opponent = matches[me - 1].first
      if matches[opponent - 1].first == me
        delete_list << me
        delete_list << opponent
      end
    end
  end
  throw :break_loop if delete_list.empty?
  day += 1
  delete_list.uniq!
  until delete_list.empty?
    d = delete_list.shift
    matches[d - 1].shift
    queue << d
  end
end
end
puts day == 0 ? -1 : day
