Scanf.scanf "%d %d %d" (fun n m q ->
    let abcd = Array.init q (fun _ ->
        Scanf.scanf " %d %d %d %d" (fun a b c d -> a - 1, b - 1, c, d))
    in
    let check hist =
        let arr = Array.of_list hist in
        Array.fold_left (fun acc (a, b, c, d) ->
            if arr.(b) -  arr.(a) = c then acc + d else acc) 0 abcd
    in
    let rec take r cur hist acc =
        if r = 0 then max acc (check hist) else
            if cur = 0 then acc else
                let acc = take r (cur - 1) hist acc in
                take (r - 1) cur (cur :: hist) acc
    in
    take n m [] 0 |> Printf.printf "%d\n"
)