fun main(args: Array<String>) = println(run {
    val (n,m,q)= readLine()!!.split(" ").map { it.toInt() }
    val abcd = (1..q).map { readLine()!!.split(" ").map { it.toInt() } }

    val numsList = getNumsList(1, 0, m, IntArray(n), mutableListOf<IntArray>())
    numsList.map {
        point(abcd, it)
    }.max()!!

})

fun getNumsList(min: Int, index: Int, m: Int, array: IntArray, all: MutableList<IntArray>): List<IntArray> {
    if(index > array.lastIndex) {
        all.add(array.copyOf())
        return all
    }
    for (i in min..m) {
        array[index] = i
        getNumsList(i, index+1, m, array, all)
    }
    return all
}

fun point(abcd: List<List<Int>>, nums: IntArray) =
        abcd.sumBy {
            val (a,b,c,d) = it
            if(nums[b-1]-nums[a-1] == c) d else 0
        }
