n,m = gets.split.map(&:to_i)
a = []
b = []
c = []
m.times {
  ai,bi = gets.split.map(&:to_i)
  a << ai
  b << bi
  c << gets.split.map(&:to_i)
}
if c.flatten.uniq.count != n
  p -1
  exit
end
min = a.inject(:+)
ok = []
[false,true].repeated_permutation(m).to_a.each {|arr|
  arr_int = arr.map.with_index{|arri,i|i if arri} - [nil]
  flag = true
  ok.each {|okarr|
    if (okarr - arr_int) == []
      flag = false
      break
    end
  }
  if (flag) && (c.map.with_index{|ci, i| (arr[i] ? ci : []) }.flatten.uniq.count == n)
    cost = a.map.with_index{|ai, i| (arr[i] ? ai : 0) }.inject(:+)
    min = cost if cost<min
    ok << arr_int
  end
}
p min
