type Condition = (Int, Int, Int, Int)

main = do
  [n, m, q] <- readIntegers
  conditions <- readConditions q
  let sequences = probableSequences 1 m n
  print $ foldl max 0 $ map (evaluateSequence conditions) sequences

readIntegers :: IO [Int]
readIntegers = (map read . words) <$> getLine::IO [Int]

readConditions :: Int -> IO [Condition]
readConditions n
  | n <= 0 = return []
  | otherwise = do
    [a, b, c, d] <- readIntegers
    next <- readConditions (n - 1)
    return ((a, b, c, d) : next)

probableSequences :: Int -> Int -> Int -> [[Int]]
probableSequences _ _ 0 = [[]]
probableSequences mMin mMax n = concat $ map (\m -> map (m:) $ probableSequences m mMax (n - 1)) [mMin..mMax]

evaluateSequence :: [Condition] -> [Int] -> Int
evaluateSequence conditions sequence = foldl (+) 0 $ map (scoreForCondition sequence) conditions

scoreForCondition :: [Int] -> Condition -> Int
scoreForCondition sequence (a, b, c, d) =
  if sequence !! (b - 1) - sequence !! (a - 1) == c
    then d
    else 0
