n = gets.to_i

class Player
  attr_accessor :id, :battle_orders, :possible_game, :end

  def initialize(id, battle_orders)
    @id = id
    @battle_orders = battle_orders
    @possible_game = true
    @end = false
  end

  def next_opponent_id
    battle_orders.first - 1
  end

  def battle
    battle_orders.shift
    self.possible_game = false
    self.end = true if finish?
  end

  def finish?
    battle_orders.empty?
  end

  def reset
    self.possible_game = true if possible_game == false
  end
end

players = []
players2 = []
n.times do |i|
  player = Player.new(i, gets.split.map(&:to_i))
  players << player
  players2 << player
end

day = 0
while true
  day += 1
  flg = true
  players2.each(&:reset)
  players2.each do |player|
    next if player.end
    opponent_player = players[player.next_opponent_id]
    next if opponent_player.next_opponent_id != player.id || opponent_player.end
    if player.possible_game && opponent_player.possible_game
      flg = false
      player.battle
      opponent_player.battle
      players2.delete(player) if player.end
      players2.delete(opponent_player) if opponent_player.end
    end
  end
  if flg
    break
  end
end
if players2.size == 0
  p day - 1
else
  p -1
end