import           Control.Monad
import qualified Data.ByteString.Char8 as BS
import           Data.Char
import           Data.List
import           Data.Maybe

type SeqA = [Int]

type ABCD = (Int, Int, Int, Int)

main = do
  [n, m, q] <- readInts <$> BS.getLine
  lines <- replicateM q BS.getLine
  let abcds = map (toABCD . readInts) lines
  print $ solve n m q abcds
  where
    readInts :: BS.ByteString -> [Int]
    readInts = unfoldr (BS.readInt . BS.dropWhile isSpace)
    toABCD :: [Int] -> ABCD
    toABCD [a, b, c, d] = (a, b, c, d)

solve :: Int -> Int -> Int -> [ABCD] -> Int
solve n m q abcds = maximum $ map (score abcds) seqACands
  where
    seqACands :: [SeqA]
    seqACands = dupCombinations 1 m n

score :: [ABCD] -> SeqA -> Int
score abcds seqA = sum $ map (score seqA) abcds
  where
    score :: SeqA -> ABCD -> Int
    score seqA (a, b, c, d)
      | seqA !! (b - 1) - seqA !! (a - 1) == c = d
      | otherwise = 0

dupCombinations :: Int -> Int -> Int -> [SeqA]
dupCombinations low high 0 = [[]]
dupCombinations low high n = do
  aHead <- [low .. high]
  aTail <- dupCombinations aHead high $ n - 1
  return $ aHead : aTail
