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

let arr = Array.init q (fun _ -> Scanf.scanf "%d %d %d %d\n" @@ fun a b c d -> (a - 1, b - 1, c, d))

let ans = ref 0

let rec loop i v ts =
  if i = n then begin
    let sum = ref 0 in
    Array.iter (fun (a, b, c, d) -> sum := !sum + if ts.(b) - ts.(a) = c then d else 0) arr;
    ans := max !ans !sum
  end
  else begin
    ts.(i) <- v;
    for j = v to m do
      loop (i + 1) j ts;
    done
  end

let () =
  for i = 1 to m do
    loop 0 i @@ Array.make n 0
  done;
  print_int !ans