import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List
import Data.Bits
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    [n,m,q] <- getIntList
    qs <- replicateM q $ do
        [a,b,c,d] <- getIntList
        return (a-1,b-1,c,d)
    let xss = mkl n m
    let ans = maximum $ map (solve qs) xss
    print ans

mkl 1 m = map (:[]) [1..m]
mkl k m = [h:l |l <- mkl (k-1) m, h <- [1..head l]]

solve qs xs = foldl' (\s (a,b,c,d) -> s + if xs' VU.! b - xs' VU.! a == c then d else 0 ) 0 qs
     where xs' = VU.fromList xs