Problem:
Score : 500 points

Problem Statement

We have N locked treasure boxes, numbered 1 to N.

A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.

Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.

Constraints

All values in input are integers.

1 \leq N \leq 12

1 \leq M \leq 10^3

1 \leq a_i \leq 10^5

1 \leq b_i \leq N

1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
c_{11} c_{12} ... c_{1{b_1}}
:
a_M b_M
c_{M1} c_{M2} ... c_{M{b_M}}

Output

Print the minimum cost required to unlock all the treasure boxes.
If it is impossible to unlock all of them, print -1.

Sample Input 1

2 3
10 1
1
15 1
2
30 2
1 2

Sample Output 1

25

We can unlock all the boxes by purchasing the first and second keys, at the cost of 25 yen, which is the minimum cost required.

Sample Input 2

12 1
100000 1
2

Sample Output 2

-1

We cannot unlock all the boxes.

Sample Input 3

4 6
67786 3
1 3 4
3497 1
2
44908 3
2 3 4
2156 3
2 3 4
26230 1
2
86918 1
3

Sample Output 3

69942

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;

namespace PAST0
{
    using static Ex;
    public class Solve
    {
        public static void Main(string[] args)
        {
            new PAST_J();
        }
    }
    public class PAST_J
    {
        // 宝箱がN個 鍵M個
        int n, m;
        long ans = long.MaxValue;
        Tuple<long, HashSet<int>>[] arr;
        public PAST_J()
        {
            var l = ReadIntArray();
            n = l[0];
            m = l[1];
            arr = new Tuple<long, HashSet<int>>[m];
            for (int i = 0; i < m; i++)
            {
                // ai円で販売されており、bi種類
                var l1 = ReadLongArray();
                var a = l1[0];
                var c = ReadIntArray();
                arr[i] = new Tuple<long, HashSet<int>>(a, new HashSet<int>(c));
            }

            dp(0, 0, new HashSet<int>(Enumerable.Range(1, n)));

            if (ans == long.MaxValue) ans = -1;
            ans.Cw();
        }

        void dp(long max, long i, HashSet<int> box)
        {
            if (max > ans) return;

            if (!box.Any())
            {
                ans = max;
                return;
            }
            if (i >= m) return;


            dp(max, i + 1, new HashSet<int>(box));

            var c = box.Count;
            box.ExceptWith(arr[i].Item2);
            if (c == box.Count) return;
            dp(max + arr[i].Item1, i + 1, box);
        }
    }
    public class PAST_K
    {
        public PAST_K()
        {
        }
    }
    public class PAST_L
    {
        public PAST_L()
        {
        }
    }
    public static class Ex
    {
        public static void Cw(this object s) { Console.WriteLine(s); }



        public static bool Mins<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
        public static bool Maxs<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
        public static void Swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
        public static void Swap<T>(List<T> list, int i, int j) { T tmp = list[j]; list[j] = list[i]; list[i] = tmp; }


        static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); }
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); }
        public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); }
        public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); }
        public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); }

        public static string Read { get { return Console.ReadLine().Trim(); } }
        public static int ReadInt { get { return int.Parse(Read); } }
        public static long ReadLong { get { return long.Parse(Read); } }
        public static double ReadDouble { get { return double.Parse(Read); } }
        public static string[] ReadArray() { return Read.Split(' '); }
        public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); }
        public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); }
        public static string[] ReadArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
        public static int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
        public static long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }


        public class PriorityQueue<T> where T : IComparable<T>
        {
            List<T> list = new List<T>();
            void PushHeap(T v)
            {
                int n = list.Count;
                list.Add(v);

                while (n != 0)
                {
                    int i = (n - 1) / 2;
                    if (list[n].CompareTo(list[i]) > 0)
                        Swap(list, i, n);
                    n = i;
                }
            }
            void PopHeap()
            {
                int n = list.Count - 1;
                list[0] = list[n];
                list.RemoveAt(n);

                for (int i = 0, j; (j = 2 * i + 1) < n;)
                {
                    if ((j != n - 1) && (list[j].CompareTo(list[j + 1]) < 0))
                        j++;
                    if (list[i].CompareTo(list[j]) < 0)
                        Swap(list, i, j);
                    i = j;
                }
            }
            public void Push(T elem) { PushHeap(elem); }
            public T Pop() { var v = Peek; PopHeap(); return v; }
            public T Peek { get { return list[0]; } }
            public int Count { get { return list.Count; } }
        }

    }
    public struct Mod : IEquatable<Mod>, IEquatable<long>
    {
        static public long _mod = 1000000007; long v;
        public Mod(long x) { if (x < _mod && x >= 0) v = x; else if ((v = x % _mod) < 0) v += _mod; }
        static public implicit operator Mod(long x) => new Mod(x);
        static public implicit operator long(Mod x) => x.v;
        public void Add(Mod x) { if ((v += x.v) >= _mod) v -= _mod; }
        public void Sub(Mod x) { if ((v -= x.v) < 0) v += _mod; }
        public void Mul(Mod x) => v = (v * x.v) % _mod;
        public void Div(Mod x) => v = (v * Inverse(x.v)) % _mod;
        static public Mod operator +(Mod x, Mod y) { var t = x.v + y.v; return t >= _mod ? new Mod { v = t - _mod } : new Mod { v = t }; }
        static public Mod operator -(Mod x, Mod y) { var t = x.v - y.v; return t < 0 ? new Mod { v = t + _mod } : new Mod { v = t }; }
        static public Mod operator *(Mod x, Mod y) => x.v * y.v;
        static public Mod operator /(Mod x, Mod y) => x.v * Inverse(y.v);
        static public bool operator ==(Mod x, Mod y) => x.v == y.v;
        static public bool operator !=(Mod x, Mod y) => x.v != y.v;
        static public long Inverse(long x) { long b = _mod, r = 1, u = 0, t = 0; while (b > 0) { var q = x / b; t = u; u = r - q * u; r = t; t = b; b = x - q * b; x = t; } return r < 0 ? r + _mod : r; }
        public bool Equals(Mod x) => v == x.v;
        public bool Equals(long x) => v == x;
        public override bool Equals(object x) => x == null ? false : Equals((Mod)x);
        public override int GetHashCode() => v.GetHashCode();
        public override string ToString() => v.ToString();
        static List<Mod> _fact = new List<Mod>() { 1 };
        static void B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); }
        static public Mod Comb(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)] / _fact[(int)k]; }
        static public Mod Perm(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)]; }
    }
}