N = gets.to_i
players = N.times.map { gets.split.map{|i| i.to_i - 1} }

counter = 0
break_counter = N * (N - 1) / 2
while true
  counter += 1
  lock = Array.new(N, false)
  flag = false

  opponent_ids = players.map{ |player| player.first }

  opponent_ids.each_with_index do |opponent_id, player_id|
    next if opponent_id.nil?

    if opponent_ids[opponent_id] == player_id && !lock[player_id] && !lock[opponent_id]
      flag = true
      players[player_id].shift
      players[opponent_id].shift
      lock[player_id] = true
      lock[opponent_id] = true
      break_counter -= 1
    end
  end

  if !flag
    puts '-1'
    exit
  end

  break if break_counter.zero?
end

puts counter