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 = []
n.times do |i|
  players << Player.new(i, gets.split.map(&:to_i))
end

day = 0
until players.all?(&:end)
  day += 1
  flg = true
  players.each(&:reset)
  players.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
    end
  end
  if flg
    p -1
    exit
  end
end
p day