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 Python (3.4.3) (judge verdict: Time Limit Exceeded)

#142e
N,M = map(int, input().split())

openable_box_and_price = {} #開けられる箱のtaple:値段 なdict

#同じ箱を開けられる鍵は最安値のものを選びつつ、読み込み
for i in range(M):
    a,b = map(int, input().split())
    c = tuple(map(int, input().split()))
    
    #print(c,":",a)
    
    if c in openable_box_and_price:
        #print("重複")
        if openable_box_and_price[c] > a:
            #print("安値更新")
            openable_box_and_price[c] = a
    else:
        openable_box_and_price[c] = a

#鍵に番号(0～)を付けるためlist化
key_price = list(openable_box_and_price.values())
key_openable_box = list(openable_box_and_price.keys())

#ある箱を開けられる鍵のリストを作る
key_number = 0
box_and_openable_key = [[] for _ in range(N)]

for openable_boxes in key_openable_box:
    for openable_box in openable_boxes:
        box_and_openable_key[openable_box-1].append(key_number)

    key_number += 1

#すべての箱を開けられる鍵の組み合わせのうち、最安値を求める。
no_ans_flag = 0

#そもそも開けられる鍵が無い箱があったら-1を出力
for openable_key in box_and_openable_key:
    if not openable_key:
        print(-1)
        no_ans_flag = 1
        break
        
if no_ans_flag != 1:
    def choice_keys(box_number):
        for key_number in box_and_openable_key[box_number]: #箱を開けられる鍵から1個選ぶ
            choiced_keys.append(key_number)
            if len(choiced_keys) != N: #すべての箱に対して鍵を選んでなかったら
                choice_keys(box_number+1) #さらに選ぶ
            else: #すべての箱に対して鍵を選んでたら
                #値段を計算する
                
                choiced_keys_set = set(choiced_keys) #重複選択している鍵を削除する

                if tuple(choiced_keys_set) not in caluclated_key_set_and_price: #setでは辞書のkeyに使えないので、tuple化
                    #値段計算
                    total_price = 0
                    for choiced_key in choiced_keys_set:
                        total_price += key_price[choiced_key]

                    caluclated_key_set_and_price[tuple(choiced_keys_set)] = total_price

                    #print(choiced_keys,":",choiced_keys_set,":",total_price)

                choiced_keys.pop(-1)

        if len(choiced_keys)!=0:
            choiced_keys.pop(-1)        

           
    caluclated_key_set_and_price = {}
    choiced_keys = []
    choice_keys(0)

    #求めたすべての箱を開けられる鍵の選択肢一覧から最安を選ぶ
    #print(caluclated_key_set_and_price)
    caluclated_key_set_and_price = sorted(caluclated_key_set_and_price.items(), key=lambda x:x[1])
    #print(caluclated_key_set_and_price)
    print(caluclated_key_set_and_price[0][1])