N = gets.to_i
games = Array.new (N) { gets.chomp.split.map(&:to_i) }

day = 0
# 試合が残ってる限りループ
while games.any?(&:any?)
  games_today = []

  games.each_with_index do |games_of_player, player|
    # 試合終わってたら飛ばす
    next if games_of_player.empty?
    # 試合決まってたら飛ばす
    # next if games_today.include? player
    # 試合成立
    if games[games_of_player.first - 1].first - 1 == player
      games_today << player
      # games_today << (games_of_player.first - 1)
    end
  end

  # 進行不能
  if games_today.empty?
    day = -1
    break
  end

  # 今日の試合
  games_today.each do |player|
    games[player].shift
  end

  day += 1
end

puts day
