fun main() {
    val (n, m, q) = next3Int()
    val values = Array(q) { IntArray(4) { nextInt() } }
    values.sortByDescending { it[3] }
    val a = IntArray(n) { 1 }
    var max = 0L
    while (true) {
        var sum = 0L
        for (value in values) {
            if (a[value[1] - 1] - a[value[0] - 1] == value[2]) {
                sum += value[3]
            }
        }
        max = kotlin.math.max(max, sum)
        if (a[0] == m) {
            break
        }
        for (i in n - 1 downTo 0) {
            if (a[i] < m) {
                a[i]++
                for (j in i + 1 until n) {
                    a[j] = a[i]
                }
                break
            }
        }
    }
    println(max)
}

val scanner = java.util.Scanner(System.`in`)
fun next(): String = scanner.next()
fun nextInt() = scanner.nextInt()
fun next2Int() = Pair(scanner.nextInt(), scanner.nextInt())
fun next3Int() = Triple(scanner.nextInt(), scanner.nextInt(), scanner.nextInt())
fun nextLong() = scanner.nextLong()
fun nextLine(): String = scanner.nextLine()
fun nextList() = nextLine().split(" ")
fun nextIntList() = nextList().map(String::toInt)
fun nextIntArray(size: Int) = IntArray(size) { nextInt() }
fun nextLongList() = nextList().map(String::toLong)
fun printYesNo(isYes: Boolean) = println(if (isYes) "Yes" else "No")