import java.lang.Math.max

fun main() {
    val (n, m, q) = readIntList()
    val a = Array(q + 1) { 0 }
    val b = Array(q + 1) { 0 }
    val c = Array(q + 1) { 0 }
    val d = Array(q + 1) { 0 }

    repeat(q) {
        val (i, j, k, l) = readIntList()
        a[it + 1] = i
        b[it + 1] = j
        c[it + 1] = k
        d[it + 1] = l
    }

    var ans = 0

    fun dfs(A: Array<Int>) {
        var tmp = A
        if (tmp.size == n + 1) {
            var now = 0
            for (i in 1..q) {
                if (A[b[i]] - A[a[i]] == c[i]) now += d[i]
            }
            ans = max(ans, now)
            return
        }
        tmp = tmp.plus(tmp.last())
        while (tmp.last() <= m) {
            dfs(tmp)
            val lastI = tmp.lastIndex
            tmp[lastI]++
        }
    }

    dfs(arrayOf(1))
    println(ans)
}

fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readIntList() = readLine()!!.split(" ").map(String::toInt)
fun readLongList() = readLine()!!.split(" ").map(String::toLong)
fun <T> Array<Array<T>>.print() {
    this.forEach {
        println(it.joinToString(" "))
    }
}