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

patterns :: Int -> Int -> [[Int]]
patterns n m = do
  c <- [1..m]
  f [c] n
  where 
    f xs 1 = [xs]
    f t@(x:xs) n = do 
      p <- [1..x] 
      f (p:t) (n - 1)

score :: Array Int Int -> [(Int, Int, Int, Int)] -> Int
score xs [] = 0
score xs ((a,b,c,d):qs) = if (xs ! b - xs ! a == c) then d + score xs qs
                                                    else score xs qs

search :: [[Int]] -> [(Int,Int,Int,Int)] -> Int -> Int
search xss qs n = foldl (\x y -> let s = score (listArray (1, n) y) qs in if s > x then s else x ) 0 xss

main = do 
  (n:m:q:_) <- (fmap read . words) <$> getLine :: IO [Int]
  qs <- replicateM q $ do 
        (a:b:c:d:_) <- (fmap read . words) <$> getLine
        return (a,b,c,d) :: IO (Int, Int, Int, Int)
  print $ search (patterns n m) qs n
