let n, m, q = Scanf.scanf "%d %d %d\n" (fun n m q -> n, m, q)

let a = Array.make q 0
let b = Array.make q 0
let c = Array.make q 0
let d = Array.make q 0

let rec f i =
  if i < q then (
      Scanf.scanf "%d %d %d %d\n" (fun w x y z ->
      a.(i) <- w - 1;
      b.(i) <- x - 1;
      c.(i) <- y;
      d.(i) <- z
    );
    f (i + 1)
  )

let rec g i j e ans =
  if j = n then (
      let rec h i ans =
        if i = q then ans
        else h (i + 1) (ans + if e.(b.(i)) - e.(a.(i)) = c.(i) then d.(i) else 0)
      in h 0 0
  ) else (
      let rec h i ans =
        if i = m + 1 then ans
        else (
            e.(j) <- i;
            h (i + 1) (max ans (g i (j + 1) e ans))
        )
      in h i 0
  )

let () =
  f 0;
  let ans = g 1 0 (Array.make n 0) 0 in
  Printf.printf "%d\n" ans
