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

var line = readIntArray()
let (N, M, Q) = (line[0], line[1], line[2])

var queries: [[Int]] = (0..<Q).map({_ in readIntArray()})
var ans = 0

func dfs(_ A: [Int]) {
    if A.count == (N + 1) {
        ans = max(ans, score(A))
        return
    }
    
    var last = A.last!
    while last <= M {
        dfs(A + [last])
        last += 1
    }
}

func score(_ A: [Int]) -> Int {
    var score = 0
    (0..<Q).forEach({i in
        let query = queries[i]
        let (a, b, c, d) = (query[0], query[1], query[2], query[3])
        if (A[b] - A[a] == c) {
            score += d
        }
    })
    return score
}

dfs([1])
print(ans)
