// Many Requirements

class RepeatedCombination<T>: Sequence, IteratorProtocol {
    var state: Array<Int>?
    
    func next() -> Array<T>? {
        var ret: Array<T> = []
        guard let currentState = self.state else {
            return nil
        }
        for index in currentState {
            ret.append(baseArray[index])
        }
        self.state = nextState()
        return ret
    }
    
    private func nextState() -> Array<Int>? {
        guard var currentState = self.state else {
            return nil
        }
        for i in 0..<self.takes {
            if currentState[takes-i-1] != self.n - 1 {
                let n = currentState[takes-i-1] + 1
                for j in (takes-i-1)..<self.takes {
                    currentState[j] = n
                }
                return currentState
            }
        }
        return nil
    }
    
    typealias Element = Array<T>
    
    let baseArray: Array<T>
    let takes: Int
    let n: Int
    
    
    init(_ baseArray: Array<T>, _ takes: Int){
        self.baseArray = baseArray
        self.takes = takes
        self.n = baseArray.count
        self.state = Array<Int>(repeating: 0, count: takes)
    }
}


func main() {
    let nmq = readLine()!.split(separator: " ").map{Int($0)!}
    let (n,m,q) = (nmq[0], nmq[1], nmq[2])
    var abcd: [[Int]] = []
    for _ in 1...q {
        abcd.append(readLine()!.split(separator: " ").map{Int($0)!})
    }
    let arr = Array(1...m)
    let rcombi = RepeatedCombination(arr, n)
    // Aの候補を全部試す
    var max = 0
    for ar in rcombi {
        // 得点計算
        var score = 0
        for i in 0..<q {
            let abcdi = abcd[i]
            let (ai, bi, ci, di) = (abcdi[0], abcdi[1], abcdi[2], abcdi[3])
            if ar[bi-1] - ar[ai-1] == ci {
                score += di
            }
        }
        if score > max {
            max = score
        }
    }
    print(max)
}

main()