n, m = gets.chomp.split(" ").map(&:to_i)

min = []
for i in 0...n do
	min[i] = []
end

for i in 0...m do
	a, b = gets.chomp.split(" ").map(&:to_i)
	c = gets.chomp.split(" ").map(&:to_i)
	for j in c do
		min[j - 1].push(a)
	end
end
min = min.sort do |a, b|
	if a.nil? then
		1
	elsif b.nil? then
		-1
	else
		a.length <=> b.length
	end
end

results = []
for cost in min do
	if cost.length < 1 then
		break
	else
		flag = false
		for j in results do
			if cost.include?(j) then
				flag = true
				break
			end
		end
		if flag == false then
			results.push(cost.min)
		end
	end
end
p results.length > 0 ? results.inject(:+) : -1
