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

counter = 0
while true
  counter += 1
  lock = Array.new(N, false)
  flag = false
  players.count.times do |player_id|
    opponent_id = players[player_id].first
    next if opponent_id.nil?

    if players[opponent_id].first == 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
    end
  end

  if !flag
    puts '-1'
    exit
  end

  break_flag = true
  players.each do |player|
    break_flag = false unless player.empty?
  end
  break if break_flag
end

puts counter