fun main(args: Array<String>) = ABC165C.main()

object ABC165C {
    fun log(message: Any?) {
        println(message)// TODO: ←消す
    }

    fun main() {
        val (N, M, Q) = readInts()
        val tuples = (1..Q).map { readInts() }  // a,b,c,d

        val arrays = (1..M).combination(N)
            //.onEach { log(it) }
            .toList()

        val scores = arrays.map { it.calcScore(tuples) }
        val ans = scores.max()

        println(ans)
    }

    fun List<Int>.calcScore(tuples: List<List<Int>>): Long {
        return tuples.map { (a, b, c, d) ->
            if (this[b-1]-this[a-1] == c) d.toLong() else 0L
        }.sum()
    }

    /*####################################################################################*/

    fun readString() = readLine()!!
    fun readStrings() = readLine()!!.split(" ")
    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map { it.toInt() }


    class Combination<T> : BasicCombinator<T>() {
        override fun List<T>.chooseRemains(i: Int): List<T> = this.drop(i)
    }

    fun IntRange.combination(k: Int): Sequence<List<Int>> = this.toList().combination(k)
    fun <T> List<T>.combination(k: Int): Sequence<List<T>> = Combination<T>().generate(this, k)

    abstract class BasicCombinator<T> {
        abstract fun List<T>.chooseRemains(i: Int): List<T>

        fun generate(items: List<T>, k: Int): Sequence<List<T>> {
            when {
                (k < 0) ->
                    throw IllegalArgumentException()
                (k == 0) ->
                    return emptySequence()
                (k == 1) ->
                    return items.asSequence().map { listOf(it) }
                else ->
                    return items.asSequence().mapIndexed { index, first ->
                        val remains = items.chooseRemains(index)
                        generate(remains, k - 1).map { first + it }
                    }.flatten()
            }
        }

        private operator fun T.plus(other: List<T>): List<T> {
            val list = mutableListOf(this)
            list.addAll(other)
            return list
        }
    }
}