let input = readLine()!.split(separator: " ").map { Int($0)! }
let N = input[0]
let M = input[1]
let Q = input[2]

var groups = [(Int, Int, Int, Int)]()
for _ in 0 ..< Q {
    let line = readLine()!.split(separator: " ").map { Int($0)! }
    groups.append((line[0], line[1], line[2], line[3]))
}

func depthFirstSearchHighScore(_ array: [Int] = []) -> Int {
    guard array.count <= N else {
        var score = 0
        for (a, b, c, d) in groups {
            if array[b - 1] -  array[a - 1] == c {
                score += d
            }
        }
        return score
    }

    var score = 0
    for i in (array.last ?? 1) ... M {
        let newArray = array + [i]
        score = max(score, depthFirstSearchHighScore(newArray))
    }
    return score
}

print(depthFirstSearchHighScore())
