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 ans =
    let total (arr: int []) =
        [ 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 rec dfs (arr: int []) size =
        if size = n then
            total arr
        else
            let h =
                if size = 0 then 1 else arr.[size - 1]
            [ h .. m ]
            |> List.map (fun i ->
                arr.[size] <- i
                dfs (arr) (size + 1))
            |> List.max

    dfs (Array.init n (fun _ -> 0)) 0

ans |> printfn "%d"
