fun main(args:Array<String>){
    val l = readLine()!!.split(" ").map {it.toInt()}
    val n = l[0]
    val m = l[1]
    val q = l[2]
    val a = Array(q){0}
    val b = Array(q){0}
    val c = Array(q){0}
    val d = Array(q){0}
    (0..q-1).map {
        val ll = readLine()!!.split(" ").map {it.toInt()}
        a[it] = ll[0]-1
        b[it] = ll[1]-1
        c[it] = ll[2]
        d[it] = ll[3]
    }
    var result = 0L
    fun dfs(arg:Pair<Int,Int>,array:Array<Int>){
        val current = arg.first
        val max = arg.second
        //実装部分
        var sum = 0L
        (0..q-1).map{
            if(array[b[it]]-array[a[it]]==c[it]){
                sum += d[it]
            }
        }
        if(sum>result)result=sum
        if(current>0){
            val copy = array.copyOf()
            copy[current-1]++
            if(copy[current-1]<=copy[current]) {
                dfs(Pair(current - 1, copy[current]), copy)
            }
        }
        if(array[current]<max){
            val copy = array.copyOf()
            copy[current]++
            dfs(Pair(current,max),copy)
        }
    }
    dfs(Pair(n-1,m),Array(n){1})
    println(result)
}