import java.util.*

fun main(args : Array<String>) {
    val (n, m, q) = readLine()!!.split(" ").map { it.toInt() }
    val qq = mutableListOf<Item>()

    repeat(q) {
        val (a, b, c, d) = readLine()!!.split(" ").map { it.toInt() }
        qq.add(Item(a-1, b-1, c, d))
    }

    val dfs = Stack<IntArray>()
    dfs.push(IntArray(n){0})

    var max = 0

    while (dfs.isNotEmpty()) {
        val a = dfs.pop()
        if (a.last() != 0) {
            var score = 0
            qq.forEach {
                if (a[it.idx2] - a[it.idx1] == it.diff) score += it.score
            }
            max = Math.max(max, score)
            continue
        }

        if (a[0] == 0) {
            for (i in 1..m) {
                val newA = a.copyOf()
                newA[0] = i
                dfs.push(newA)
            }
            continue
        }

        val lastIdx = a.indexOfLast { it != 0 }
        for (i in a[lastIdx]..m) {
            val newA = a.copyOf()
            newA[lastIdx+1] = i
            dfs.push(newA)
        }
    }

    println(max)
}

data class Item(val idx1: Int, val idx2: Int, val diff: Int, val score: Int)