n, m = gets.split.map(&:to_i)
cc = {}
key = []
m.times do |i|
  a, _b = gets.split.map(&:to_i)
  key[i] = a
  gets.split.map(&:to_i).each do |c|
    if cc[c]
      cc[c] = i if key[cc[c]] > a
    else
      cc[c] = i
    end
  end
end
if cc.size < n
  p(-1)
  exit
end
p cc.values.uniq.map{|i| key[i]}.inject(:+)
