import java.util.*
import kotlin.math.*

private val readString: ()->String = {readLine()!!}
private val readInt: ()->Int = { readLine()!!.toInt() }
private val readLong: ()->Long = { readLine()!!.toLong() }
private val readListInt: ()->List<Int> = { readLine()!!.split(' ').map(String::toInt) }
private val errPrint: (String)->Unit = { msg -> System.err.println(msg) }


fun main(args: Array<String>) {
    val (N, M,Q) = readListInt()

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

    var rows = ArrayList<Row>()
    repeat(Q) {
        val (a, b, c, d) = readListInt()
        rows.add(Row(a-1,b-1,c,d))
    }

    var A = Array<Int>(N){1}

    fun sum(A: Array<Int>): Int {
        var res = 0
        for (row in rows) {
            if (A[row.b] - A[row.a] == row.c) {
                res += row.d
            }
        }
        return res
    }

    var ans = 0
    fun dfs(i:Int, x:Int) {
        if (i<0) {
            ans = max(ans, sum(A))
            return
        }

        A[i] = x

        for (nx in x downTo 1) {
            dfs(i-1, nx)
        }
    }

    dfs(N-1, M)

    println(ans)
}
