import Data.Array

calcScore :: [[Int]] -> [Int] -> Int
calcScore ctx as = do
    let ras = reverse as
    sum $ map (\ [a,b,c,d] -> if ras!!(b-1) - ras!!(a-1) == c then d else 0) ctx


solve :: Int -> Int -> [[Int]] -> [Int] -> Int
solve n m ctx as = if length as == n
                      then calcScore ctx as
                      else maximum $ map (solve n m ctx) $ map (flip (:) as) [(if as==[] then 1 else head as)..m]

main = do
    [n,m,q] <- map read . words <$> getLine :: IO[Int]
    co <- getContents
    let ctx = map (map read . words) $ lines co :: [[Int]]
    print $ solve n m ctx []
