open System

let n, m, q =
    let l = Console.ReadLine().Split(' ') |> Array.map int
    l.[0], l.[1], l.[2]

let abcd =
    [ 1 .. q ]
    |> List.map (fun _ ->
        let l = Console.ReadLine().Split(' ') |> Array.map int
        l.[0], l.[1], l.[2], l.[3])

let total (arr: int List) =
    [ 0 .. q - 1 ]
    |> List.map (fun i ->
        let a, b, c, d = abcd.[i]
        if arr.[b - 1] - arr.[a - 1] = c then d else 0)
    |> List.sum

let ans =
    let rec dfs (l: int List) =
        if l.Length = n then
            total l
        else
            let len = l.Length

            let h =
                if len = 0 then 1 else l.[len - 1]
            [ h .. m ]
            |> List.map (fun i -> dfs (l @ [ i ]))
            |> List.max
    dfs []

ans |> printfn "%d"
