n = gets.to_i
constraints = n.times.map do
  gets.split.map(&:to_i)
end

def solve(cs, today_players = [], day = 1)
  if cs.all?(&:empty?)
    return day
  end

  answers = cs.each.with_index(1).map do |player_cs, player|
    next if player_cs.empty?

    opponent = player_cs.first
    opponent_cs = cs[opponent - 1]
    if opponent_cs.first == player
      cs[player - 1] = player_cs[1..-1] || []
      cs[opponent - 1] = opponent_cs[1..-1] || []
      if today_players.include?(player) || today_players.include?(opponent)
        solve(cs, [player, opponent], day + 1)
      else
        today_players = today_players.dup.push(player, opponent)
        solve(cs, today_players, day)
      end
    end
  end

  answers.compact.min
end

answer = solve(constraints)
puts answer || '-1'