import java.util.*

fun main() {
    val l = readLine()!!.split(' ').map { it.toInt() }
    val a = Array(l[2]) { 0 }
    val b = Array(l[2]) { 0 }
    val c = Array(l[2]) { 0 }
    val d = Array(l[2]) { 0 }
    for (i in 0 until l[2]) {
        val line = readLine()!!.split(' ').map { it.toInt() }
        a[i] = line[0]-1
        b[i] = line[1]-1
        c[i] = line[2]
        d[i] = line[3]
    }

    fun eval(list: List<Int>): Int {
        var res = 0
        for (x in 0 until a.size) {
            if (list[b[x]]-list[a[x]] == c[x]) {
                res += d[x]
            }
        }
        return res
    }

    var max = 0
    fun dfs(list: List<Int>) {
        if (list.size < l[0]) return (list.last()..l[1]).forEach { dfs(list + it) }
        max = Math.max(max, eval(list))
    }

    dfs(listOf(1))
    println(max)
}

