def ee
    n,m = gets.chomp.split(/\s/).map{|x| x.to_i}

    a = []
    c = []
    for i in 1..m
        a.push(gets.chomp.split(/\s/).map{|x| x.to_i}[0])
        c.push(gets.chomp.split(/\s/).map{|x| x.to_i})
    end
    p c

    min = 100000+1
    for i in 0..(c.length-1)
        for j in (i+1)..(c.length-1)
            if c[i].product(c[j]).flatten.uniq.length == n
                min = [a[i] + a[j], min].min
            end
        end
    end

    if min == 100000+1
        puts -1
    else
        puts min
    end
end

ee