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

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 (point abcds) seqACands
  where
    seqACands :: [SeqA]
    seqACands = gen 1 m n
      where
        gen :: Int -> Int -> Int -> [SeqA]
        gen low high 0 = map (: []) [low .. high]
        gen low high n = do
          aHead <- [low .. high]
          aTail <- gen aHead high $ n - 1
          return $ aHead : aTail

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

type SeqA = [Int]

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