{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns          #-}
{-# LANGUAGE CPP                   #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MagicHash             #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE MultiWayIf            #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE ViewPatterns          #-}

import           Control.Applicative
import           Control.Exception
import           Control.Monad
import           Control.Monad.Primitive
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Data.Bool
import qualified Data.ByteString             as B
import qualified Data.ByteString.Builder     as B
import qualified Data.ByteString.Char8       as C
import qualified Data.ByteString.Internal    as B
import qualified Data.ByteString.Unsafe      as B
import           Data.Char
import qualified Data.Foldable               as F
import           Data.Function
import qualified Data.IntMap.Strict          as IM
import qualified Data.IntSet                 as IS
import qualified Data.List                   as L
import qualified Data.Map.Strict             as M
import           Data.Monoid
import           Data.Ord
import           Data.Primitive.MutVar
import qualified Data.Set                    as S
import           Data.Tuple
import qualified Data.Vector                 as V
import qualified Data.Vector.Mutable         as VM
import qualified Data.Vector.Generic         as G
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Unboxed         as U
import qualified Data.Vector.Unboxed.Mutable as UM
import           Debug.Trace
import           Foreign                     hiding (void)
import           GHC.Exts
import qualified System.IO                   as IO
import           Unsafe.Coerce

main :: IO ()
main = do
    [n, m] <- map read.words <$> getLine :: IO [Int]
    edges <- V.replicateM m $ do
        [!a, !b] <- map read.words <$> getLine :: IO [Int]
        xs <- U.unfoldrN b parseInt <$> C.getLine
        return (a, b, xs)
    print . maybe (-1) id $ solve n m edges

solve :: Int -> Int -> V.Vector (Int, Int, U.Vector Int) -> Maybe Int
solve n m inputs
    | dist U.! dst == inf = Nothing
    | otherwise = Just $ dist U.! dst
  where
    numV = 2 ^ n
    numE = U.length edges
    src = 0
    dst = 2 ^ n - 1
    dist = dijkstra numE src $ fromEdges numV edges
    edges = V.convert . V.concatMap id
        $ V.generate numV $ \st ->
            V.filter (\(x,y,_)->x/=y) . flip V.map inputs $ \(cost, _, boxes) ->
                let st' = U.foldl' (\acc i -> setBit acc (i - 1)) st boxes
                in (st, st', cost)
fromEdges' :: Int -> U.Vector Edge -> Graph
fromEdges' numV edges = V.map (U.fromList . map minimum . L.groupBy ((==) `on` fst). L.sort)
    . V.unsafeAccumulate (flip (:)) (V.replicate numV [])
    . U.convert
    . U.map (\(src, dst, cost) -> (src, (dst, cost)))
    $ edges
------------------------------------------------------------------------------
data HeapM m a = HeapM (MutVar m Int) (UM.MVector m a)

newHeap :: (PrimMonad m, U.Unbox a) => Int -> m (HeapM (PrimState m) a)
newHeap limitSize = HeapM `liftM` newMutVar 0 `ap` UM.new limitSize

getHeapSize :: (PrimMonad m) => HeapM (PrimState m) a -> m Int
getHeapSize (HeapM ref _) = readMutVar ref
{-# INLINE getHeapSize #-}

insertM :: (PrimMonad m, U.Unbox a, Ord a)
        => a -> HeapM (PrimState m) a -> m ()
insertM x heap@(HeapM ref vec) = do
    size <- getHeapSize heap
    modifyMutVar ref (+1)
    flip fix size $ \loop !i ->
        if i == 0
        then UM.unsafeWrite vec 0 x
        else do
            let !parent = (i - 1) `unsafeShiftR` 1
            p <- UM.unsafeRead vec parent
            if p <= x
            then UM.unsafeWrite vec i x
            else do
                UM.unsafeWrite vec i p
                loop parent
{-# INLINE insertM #-}

unsafeDeleteMinM :: (PrimMonad m, U.Unbox a, Ord a)
                 => HeapM (PrimState m) a -> m ()
unsafeDeleteMinM (HeapM ref vec) = do
    modifyMutVar' ref (subtract 1)
    size <- readMutVar ref
    x <- UM.unsafeRead vec size
    flip fix 0 $ \loop !i -> do
        let !l = unsafeShiftL i 1 .|. 1
        if size <= l
        then UM.unsafeWrite vec i x
        else do
            let !r = l + 1
            childL <- UM.unsafeRead vec l
            childR <- UM.unsafeRead vec r
            if r < size && childR < childL
            then if x <= childR
                 then UM.unsafeWrite vec i x
                 else do
                     UM.unsafeWrite vec i childR
                     loop r
            else if x <= childL
                 then UM.unsafeWrite vec i x
                 else do
                     UM.unsafeWrite vec i childL
                     loop l
{-# INLINE unsafeDeleteMinM #-}

unsafeMinViewM :: (PrimMonad m, U.Unbox a) => HeapM (PrimState m) a -> m a
unsafeMinViewM (HeapM _ vec) = UM.unsafeRead vec 0
{-# INLINE unsafeMinViewM #-}

minViewM :: (PrimMonad m, U.Unbox a) => HeapM (PrimState m) a -> m (Maybe a)
minViewM heap = do
    size <- getHeapSize heap
    if size > 0
    then Just `liftM` unsafeMinViewM heap
    else return Nothing
{-# INLINE minViewM #-}

deleteFindMinM :: (PrimMonad m, U.Unbox a, Ord a)
               => HeapM (PrimState m) a -> m (Maybe a)
deleteFindMinM heap = do
    size <- getHeapSize heap
    if size > 0
    then liftM2 ((Just.).const) (unsafeMinViewM heap) (unsafeDeleteMinM heap)
    else return Nothing
{-# INLINE deleteFindMinM #-}

clearHeapM :: (PrimMonad m) => HeapM (PrimState m) a -> m ()
clearHeapM (HeapM ref _) = writeMutVar ref 0
{-# INLINE clearHeapM #-}

type Vertex = Int
type Cost = Int
type Edge = (Vertex, Vertex, Cost)
type Graph = V.Vector (U.Vector (Vertex, Cost))

reverseEdge :: Edge -> Edge
reverseEdge (src, dst, cost) = (dst, src, cost)

toUndirectedEdges :: U.Vector Edge -> U.Vector Edge
toUndirectedEdges edges = edges U.++ U.map reverseEdge edges

fromEdges :: Int -> U.Vector Edge -> Graph
fromEdges numV edges = V.map U.fromList
    . V.unsafeAccumulate (flip (:)) (V.replicate numV [])
    . U.convert
    . U.map (\(src, dst, cost) -> (src, (dst, cost)))
    $ edges

inf :: Cost
inf = 0x3f3f3f3f

dijkstra :: Int -> Vertex -> Graph -> U.Vector Cost
dijkstra numE root gr = U.create $ do
    dist <- UM.replicate (V.length gr) inf
    heap <- newHeap numE
    UM.unsafeWrite dist root 0
    insertM (0, root) heap
    fix $ \loop -> do
        hd <- deleteFindMinM heap
        case hd of
            Just (!d,!v) -> do
                dv <- UM.unsafeRead dist v
                if d > dv
                then loop
                else do
                    U.forM_ (V.unsafeIndex gr v) $ \(nv, v2nv) -> do
                        let !dnv = dv + v2nv
                        old <- UM.unsafeRead dist nv
                        when (dnv < old) $ do
                            UM.unsafeWrite dist nv dnv
                            insertM (dnv, nv) heap
                    loop
            Nothing -> return dist

unsafeModify :: (PrimMonad m, GM.MVector mv a)
             => mv (PrimState m) a -> Int -> (a -> a) -> m ()
unsafeModify v i f = GM.unsafeRead v i >>= GM.unsafeWrite v i . f
{-# INLINE unsafeModify #-}


rep :: Monad m => Int -> (Int -> m ()) -> m ()
rep !n = U.forM_ $ U.generate n id
{-# INLINE rep #-}

rev :: Monad m => Int -> (Int -> m ()) -> m ()
rev !n = U.forM_ $ U.iterateN n (subtract 1) (n - 1)
{-# INLINE rev #-}


type Parser a = C.ByteString -> Maybe (a, C.ByteString)

parseInt :: Parser Int
parseInt = C.readInt . C.dropWhile isSpace

parseInt2 :: Parser (Int, Int)
parseInt2 = runStateT $
    (,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)

parseInt3 :: Parser (Int, Int, Int)
parseInt3 = runStateT $
    (,,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)

parseInt4 :: Parser (Int, Int, Int, Int)
parseInt4 = runStateT $
    (,,,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)

Measured memory_bytes: 514940