n = gets.to_i
players = n.times.map { gets.split.map(&:to_i) }

count = 0
until players.all?(&:empty?)
  today_game = []
  play_game_player_ids = []
  players.each.with_index(1) do |player, id|
    next if player.empty?
    f = player.first
    g = [f, id].sort
    if today_game.include?(g)
      play_game_player_ids.concat(g)
    else
      today_game << g
    end
  end
  
  if play_game_player_ids.empty?
    puts -1
    exit
  end

  play_game_player_ids.uniq.each do |i|
    players[i-1].shift
  end
  count += 1
end
puts count