import Data.Array
import Data.List
import Control.Monad

dfs :: Int -> Int -> [(Int, Int, Int, Int)] -> Array Int Int -> Int -> Int
dfs n m qs arr i
  | i == n = foldl' (+) 0 $ map (\(a, b, c, d) -> if (arr ! b) - (arr ! a) == c then d else 0) qs
  | otherwise = maximum [dfs n m qs (arr // [(i + 1, x)]) (i + 1) | x <- [arr ! i .. m]]

main :: IO ()
main = do
  [n, m, q] <- map read . words <$> getLine
  qs <- replicateM q $ (\line -> let [a, b, c, d] = map read $ words line in (a, b, c, d)) <$> getLine
  putStrLn $ show $ dfs n m qs (array (0, n) [(0, 1)]) 0