import Foundation

private func readInt() -> [Int]{
    return readLine()!.split(separator: " ").map{Int($0)!}
}

var queries: [(Int, Int, Int, Int)]!
var N: Int!
var M: Int!
var ans: Int = 0

func dfs(_ now: Int, _ i: Int, _ comb: [Int]) {
    guard i < N else { return }
    
    var comb = comb
    for j in (now...M).reversed() {
        comb[i] = j
        var x = 0
        for (a, b, c, d) in queries {
            if comb[b] - comb[a] == c {
                x += d
            }
        }
        ans = max(x, ans)
        dfs(j, i+1, comb)
    }
}

func mainC() {
    let line = readInt()
    N = line[0]
    M = line[1]
    let Q = line[2]
    
    queries = (1...Q).map { _ in
        let q = readInt()
        return (q[0]-1, q[1]-1, q[2], q[3])
    }
    
    dfs(1, 0, [Int](repeating: M, count: N))
    print(ans)
}

mainC()
