{-# LANGUAGE PartialTypeSignatures #-}
module Main (main) where

import Control.Monad
import qualified Data.ByteString.Char8 as C8
import qualified Data.Char as C
import qualified Data.List as L

main :: IO ()
main = do
  [n,m,q] <- readInts
  replicateM q readInts >>= output . solve n m

solve :: Int -> Int -> [[Int]] -> _
solve n m xs = fst $ maximum $  map (\v -> (f v, v)) $ gen m n 1
  where
    f vec = sum $ map (\[a,b,c,d] -> if ((vec!!(b-1)) - (vec!!(a-1)))==c then d else 0) xs

    gen m 0 _ = [ [] ]
    gen m n a = [ i:is | i <- [a..m], is <- gen m (pred n) i ]

output :: _ -> IO ()
output = print

-- utils
readInts :: IO [Int]
readInts = L.unfoldr (C8.readInt . C8.dropWhile C.isSpace) <$> C8.getLine