import Control.Monad (replicateM) import Data.List (elemIndex, permutations) solve :: Integral a => a -> [(a, a, a)] -> a solve n table = maximum $ do items <- permutations [0..n - 1] return $ calc items table where calc :: Integral a => [a] -> [(a, a, a)] -> a calc items' table' = sum $ do (item1, item2, score) <- table' return $ case (compare <$> elemIndex item1 <*> elemIndex item2) items' of LT -> score GT -> 0 main :: IO () main = do [n, m] <- map read . words <$> getLine table <- replicateM m getLine let table' = do [item1, item2, score] <- map read . words <$> table return (item1, item2, score) print $ solve n table'