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

x = []

m.times do
    a,b = gets.split.map!(&:to_i)
    c = gets.split.map!(&:to_i)

    j = []
    x.each {|_,l| j = j | l}

    if (j & c).size < c.size
        x << [a,c]
    else
        y = []
        z = []
        sum = 0

        while v = x.shift do
            if v[1] & c == v[1]
                sum += v[0]
                z << v
            else
                y << v
            end
        end

        z = [[a,c]] if sum > a
        x = y + z
    end

    #p x
end

ans = 0
j = []
x.each do |v,l|
    ans += v
    j = j | l
end

puts j.size == n ? ans : -1