import java.util.*

class Scanner(`in`: java.io.InputStream) {
    private val reader = java.io.BufferedReader(java.io.InputStreamReader(`in`))
    private var tokenizer: StringTokenizer? = null

    fun next(): String {
        if (tokenizer?.hasMoreTokens() != true) {
            tokenizer = StringTokenizer(reader.readLine())
        }
        return tokenizer!!.nextToken()
    }

    fun nextLine(): String {
        return if (tokenizer?.hasMoreTokens() != true) {
            reader.readLine()
        } else tokenizer!!.nextToken("\n")
    }
}

fun main(args: Array<String>) {
    val pw = java.io.PrintWriter(System.out)
    Problem.solve(Scanner(System.`in`), pw)
    pw.flush()
}

////////////////////////////////////////////////////////////////////////

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

    lateinit var dfs: (Array<Int>) -> Unit
    var ans = 0

    fun solve(sc: Scanner, pw: java.io.PrintWriter) {
        val N = sc.next().toInt()
        val M = sc.next().toInt()
        val Q = Array(sc.next().toInt()) {
            ABCD(
                sc.next().toInt() - 1,
                sc.next().toInt() - 1,
                sc.next().toInt(),
                sc.next().toInt()
            )
        }

        dfs = { a ->
            if (a.count() < N + 1) {
                val next = (a.map { it } + a[a.count() - 1]).toTypedArray()
                while (next[next.count() - 1] <= M) {
                    dfs(next)
                    next[next.count() - 1]++
                }
            } else {
                var tmp = 0
                for (q in Q) {
                    if (a[q.b] - a[q.a] == q.c) tmp += q.d
                }
                ans = Math.max(ans, tmp)
            }
        }
        dfs(arrayOf(1))
        pw.println(ans)
    }
}
