#lang racket

(define (sum items comb)
  (let iter ([queries items])
    (if (null? queries)
        0
        (let* ([query (car queries)]
               [a (vector-ref query 0)]
               [b (vector-ref query 1)]
               [c (vector-ref query 2)]
               [d (vector-ref query 3)])
          (+ (if (= (- (vector-ref comb b) (vector-ref comb a)) c)
                 d
                 0)
             (iter (cdr queries)))))))

           
          
(define (solve N M queries ans)
  (define (dfs now i comb)
    (if (= i N)
        (void)
        (let iter ([j M])
          (if (< j now)
              (void)
              (begin
                (vector-set! comb i j)
                (set! ans (max ans (sum queries comb)))
                (dfs j (+ i 1) comb)
                (iter (- j 1)))))))
  (begin
    (dfs 1 0 (make-vector N M))
    ans))
          
          
(define (readlines q)
  (let iter ([i q])
    (if (zero? i)
        null
        (let ([a (- (read) 1)]
              [b (- (read) 1)]
              [c (read)]
              [d (read)])
          (cons (vector a b c d) (iter (- i 1)))))))

(let* ([N (read)]
       [M (read)]
       [Q (read)])
       (display (solve N M (readlines Q) 0)))