-- noch nicht fertig

import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import Data.List (unfoldr)

main :: IO ()
main = do
  [n, m, q] <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    candidates = unfoldr next $ replicate n 1 :: [[Int]]
      where
        next :: [Int] -> Maybe ([Int], [Int])
        next = fmap ((,) <$> id <*> id) . next'
          where
            next' :: [Int] -> Maybe [Int]
            next' (d : ds) | d < m     = Just $ succ d : ds
                           | otherwise = ((:) <$> head <*> id) <$> next' ds
            next' _                    = Nothing
  abcds <- sequence . replicate q $ unsafeListToTuple . map unsafeTextToInt . T.words <$> T.getLine :: IO [((Int, Int, Int), Int)]
  let
    evaluate :: [Int] -> Int
    evaluate ds = sum . map snd . filter (p . fst) $ abcds
      where
        p :: (Int, Int, Int) -> Bool
        p (x, y, z) = dy - dx == z
          where
            dy : ds' = drop (n - y) ds :: [Int]
            dx : _   = drop (pred y - x) ds' :: [Int]
  print . maximum . map evaluate $ candidates

unsafeListToTuple :: [Int] -> ((Int, Int, Int), Int)
unsafeListToTuple [w, x, y, z] = ((w, x, y), z)

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
