fun main(arr:Array<String>) {
    val (n,m,q) = readLine()!!.split(" ").map { it.toInt() }
    val req = (1..q).map { readLine()!!.split(" ").map { it.toInt() }.let { Require(it) } }
    val solve = Solve(m, req)
    var ans = 0
    for(i in (1..m)) {
        val arr = (1..n).map { 1 }.toTypedArray()
        arr[0] = i
        ans = Math.max(ans, solve.getAns(1, arr))
    }
    println(ans)
}

class Require(num:List<Int>) {
    val index1 = num[0] - 1
    val index2 = num[1] - 1
    val diff = num[2]
    val score = num[3]
}
class Solve(val maxNum:Int, val requireList:List<Require>) {
    fun getAns(index:Int, arr:Array<Int>):Int {
        if(index >= arr.size) {
            return getScore(arr)
        }

        var ans = 0
        for(i in (arr[index-1]..maxNum)) {
            arr[index] = i
            ans = Math.max(ans, getAns(index+1, arr))
        }
        return ans
    }

    fun getScore(arr:Array<Int>):Int {
        var score = 0
        for(req in requireList) {
            if(arr[req.index2] - arr[req.index1] == req.diff) {
                score+=req.score
            }
        }
        return score
    }
}
