
fun main(args: Array<String>) {
    val nmq = readLine()!!.split(" ")
    val n = nmq[0].toInt()
    val m = nmq[1].toInt()
    val q = nmq[2].toInt()
    val values = IntRange(1, q).map {
        val input = readLine()!!.split(" ")
        IntNums(input[0].toInt(), input[1].toInt(), input[2].toInt(), input[3].toInt())
    }

    var permutation = mutableListOf<MutableList<Int>>()
    for(i in 1..m) {
        createPermutation(mutableListOf(i), n, m, permutation)
    }
    val maxVal =  permutation.map { perm -> values.filter{ perm[it.b-1] - perm[it.a-1] == it.c }.map { it.d }.sum() }.max()
    print(maxVal)

}

data class IntNums(val a: Int, val b: Int, val c:Int, val d: Int)

fun createPermutation(base: MutableList<Int>, n: Int, m: Int, result: MutableList<MutableList<Int>>) {
    if(base.size == n) {
        result.add(MutableList(base.size) { i -> base[i] })
    }
    if(base.size >= n) return

    for(i in base[base.size -1]..m) {
        var newArray = MutableList(base.size) { i -> base[i] }
        newArray.add(i)
        createPermutation(newArray,n,m, result)
    }
    return
}