
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector as V
import qualified Data.ByteString.Char8 as C
import Debug.Trace
import Data.List
import Control.Monad
import Data.Ord
import qualified Data.IntSet as IS
import Data.Bits

r :: Int -> IO (U.Vector Int)
r n = U.unfoldrN n (C.readInt . C.dropWhile (==' ')) <$> C.getLine

r' :: IO [Int]
r'  =   unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine

rp :: IO (Int,Int)
rp  = toTup . unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine

toTup [a,b] = (a,b)


parseBin :: Int -> [Int] -> Int
parseBin n xs = sum $ map (\x -> 2^(n-x)) xs


main :: IO ()
main = do
    [n,m] <- r'
    input <- V.replicateM m $ do
        [a, _]<- r'
        cs <- r'
        return (a, parseBin n cs)
    let ini = U.replicate (2^n) (-1 :: Int) U.// [(0,0)]
    let x = V.foldl'
                (\dp (a,cs) -> dp U.//
                        ( U.ifoldl'
                            (\ac cs' a' ->
                                let nIdx = cs .|. cs'
                                    nVal = a + a'
                                    oVal = dp U.! nIdx
                                in (if a' /= -1 && (nVal < oVal || oVal == -1) then ((nIdx, nVal):) else id) ac
                            )
                            [] dp
                        )
                )
                ini input
    print $ U.last x

