import java.util.*


fun main() {
    val sc = Scanner(System.`in`)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val q = sc.nextInt()

    data class ABCD(val a: Int, val b: Int, val c: Int, val d: Int)

    val tuples = Array(q) { ABCD(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()) }

    var maxSums = 0
    for (listA in getAs(n, m)) {
        var sum = 0
        for ((a, b, c, d) in tuples) {
            if (listA[b-1] - listA[a-1] == c)
                sum += d
        }
        //println("${listA.contentToString()}     : $sum")
        maxSums = maxOf(maxSums, sum)
    }

    println(maxSums)
}

fun getAs(n: Int, m: Int) = sequence<IntArray> {
    fun b(ary: IntArray, prev: Int, dims: Int): Sequence<IntArray> = sequence {
        if (dims == 0) yield(ary)
        else {
            for (v in prev..m) {
                ary[ary.size - dims] = v
                yieldAll(b(ary, v, dims - 1))
            }
        }
    }
    yieldAll(b(IntArray(n), 1, n))
}
