fun str() = readLine()!!
fun read(d:String = " ") = str().split(d)
fun <T> list(d:String = " ",f:String.()->T) = read(d).map {f(it)}
fun list() = list {toInt()}

fun main() {
	val (n,m,q) = list()
	val req = Array(q) {
		list().toIntArray().also {
			--it[0]
			--it[1]
		}
	}

	val a = IntArray(n)
	fun dfs(v:Int,depth:Int):Int {
		a[depth] = v
		return when(depth) {
			n-1->req.indices.filter {a[req[it][1]]-a[req[it][0]]==req[it][2]}
					.sumBy {req[it][3]}
			else->(v..m)
					.map {dfs(it,depth+1)}.max()!!
		}
	}

	println(dfs(1,0))
}
