import java.util.*

fun main() {
    val (n, m, q) = readLine()!!.split(" ").map(String::toInt)
    val array = Array(q) {
        readLine()!!.split(" ").map(String::toInt).toIntArray()
    }
    val seq = (1..m).toList().toIntArray()
    val list = LinkedList<Int>()

    var result = 0
    fun calc(list: IntArray) {
        var sum = 0
        array.forEach { (a, b, c, d) ->
//            println("${list[b-1]} - ${list[a-1]} == $c")
            if (list[b - 1] - list[a - 1] == c) {
                sum += d
            }
        }
        result = Math.max(result, sum)
    }

    fun combination(i: Int) {

        if (list.size == n) {
//            println(list.joinToString(","))
            calc(list.toIntArray())
            return
        }

        if (i >= m) {
            return
        }

        list.add(seq[i])
        combination(i)
        combination(i + 1)
        list.removeLast()
        combination(i + 1)
    }

    combination(0)

    println(result)
}



