fun main() = abc165c()

@OptIn(ExperimentalStdlibApi::class)
fun abc165c() {

    val (n, m, q) = readLine()!!.split(' ').map { it.toInt() }
    val abcdList = (1..q).map {
        readLine()!!.split(' ').map { it.toInt() }.let { Abcd(it[0] - 1, it[1] - 1, it[2], it[3]) }
    }

    val answer = combinationsWithReplacement(listOf(1), n, m).map { list ->
        abcdList.filter { (a, b, c) -> list[b] - list[a] == c }.sumBy { it.d }
    }.max()

    println(answer)
}

private fun combinationsWithReplacement(list: List<Int>, n: Int, max: Int): List<List<Int>> {
    return if (list.size < n) (list.last()..max).map { combinationsWithReplacement(list + it, n, max) }.flatten()
    else listOf(list)
}

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