Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000)

def binize(vn):
    res = 0
    for n in vn:
        res |= 1 << (n-1)
    return res


N, M = map(int, input().split())
mK = []
for i in range(M):
    a, b = map(int, input().split())
    vc = list(map(int, input().split()))
    mK.append((a, binize(vc)))

allOpen = 2**N - 1

dA = dict()
dA[0] = [0,]
qA = [0,]

for a, c in mK:
    for aPre in qA:
        for x in dA[aPre]:
            if (x | c) in dA[aPre]:
                continue
            else:
                if (aPre + a) in qA:
                    if allOpen in dA[aPre + a]:
                        continue
                    else:
                        dA[aPre + a].append(x | c) 
                else:
                    dA[aPre + a] = [(x | c),]
                    qA.append(aPre + a)

#print(dA)
#print(max(dA.values()))
#print(allOpen)

res = 10 ** 30
for k in dA:
    if allOpen in dA[k]:
        res = min(res, k)

#print(res)
print(res if res != 10**30 else -1)


Fixed submission:
import sys
sys.setrecursionlimit(10000)
input = sys.stdin.readline

def binize(vn):
    res = 0
    for n in vn:
        res |= 1 << (n-1)
    return res


N, M = map(int, input().split())
mK = []
for i in range(M):
    a, b = map(int, input().split())
    vc = list(map(int, input().split()))
    mK.append((a, binize(vc)))

# 2**N up to 4096
allOpen = 2**N - 1
INF = float("inf")

# dp[i:keyM(<=1000)][n:openedN(<4096)] := min(sum(a))
dp = []
dp.append([0,]+[INF,]*allOpen)

#print(dp)
for na,n in mK:
	nDP = dp[-1]
	for c,pa in enumerate(dp[-1]):
		nDP[n|c] = min(nDP[n|c], pa+na)
	dp.append(nDP)

res = dp[-1][-1]
#print(res)
print(res if res != INF else -1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000)

def binize(vn):
    res = 0
    for n in vn:
        res |= 1 << (n-1)
    return res


N, M = map(int, input().split())
mK = []
for i in range(M):
    a, b = map(int, input().split())
    vc = list(map(int, input().split()))
    mK.append((a, binize(vc)))

allOpen = 2**N - 1

dA = dict()
dA[0] = [0,]
qA = [0,]

for a, c in mK:
    for aPre in qA:
        for x in dA[aPre]:
            if (x | c) in dA[aPre]:
                continue
            else:
                if (aPre + a) in qA:
                    if allOpen in dA[aPre + a]:
                        continue
                    else:
                        dA[aPre + a].append(x | c) 
                else:
                    dA[aPre + a] = [(x | c),]
                    qA.append(aPre + a)

#print(dA)
#print(max(dA.values()))
#print(allOpen)

res = 10 ** 30
for k in dA:
    if allOpen in dA[k]:
        res = min(res, k)

#print(res)
print(res if res != 10**30 else -1)


Fixed submission:
import sys
sys.setrecursionlimit(10000)
input = sys.stdin.readline

def binize(vn):
    res = 0
    for n in vn:
        res |= 1 << (n-1)
    return res


N, M = map(int, input().split())
mK = []
for i in range(M):
    a, b = map(int, input().split())
    vc = list(map(int, input().split()))
    mK.append((a, binize(vc)))

# 2**N up to 4096
allOpen = 2**N - 1
INF = float("inf")

# dp[i:keyM(<=1000)][n:openedN(<4096)] := min(sum(a))
dp = []
dp.append([0,]+[INF,]*allOpen)

#print(dp)
for na,n in mK:
	nDP = dp[-1]
	for c,pa in enumerate(dp[-1]):
		nDP[n|c] = min(nDP[n|c], pa+na)
	dp.append(nDP)

res = dp[-1][-1]
#print(res)
print(res if res != INF else -1)