fun main() {
    data class Score(val a: Int, val b: Int, val c: Int, val d: Int)
    val (n, m, q) = readLine()?.split(" ")?.map(String::toInt) ?: return
    val score = 0.until(q).map {
        val a = readLine()?.split(" ")?.map(String::toInt) ?: return
        Score(a[0] - 1, a[1] - 1, a[2], a[3])
    }

    val cList = mutableListOf<List<Int>>()
    fun generate(n: Int, m: Int, i: Int, c: List<Int>) {
        if (c.size == n) {
            cList.add(c)
        } else {
            c.last().until(m + 1).forEach {
                generate(n, m, it + 1, c + it)
            }
        }
    }
    1.until(m + 1).forEach {
        generate(n, m, it, listOf(1))
    }
    val max = cList.map { list ->
        var s = 0L
        score.forEach { score ->
            if (list[score.b] - list[score.a] == score.c) {
                s += score.d
            }
        }
        s
    }.max()
    println(max.toString())
}
