const input = require("fs").readFileSync("/dev/stdin", "utf8")
const lines = input.split('\n')
const [N, M, Q] = lines[0].split(' ').map(Number)
const q = Array(Q)
for (let i = 0; i < Q; i++) {
    q[i] = lines[i + 1].split(' ').map(Number)
}

const f = (A) => {
    let sum = 0
    for (let i = 0; i < Q; i++) {
        const a = A[q[i][0] - 1]
        const b = A[q[i][1] - 1]
        if (b - a === q[i][2]) {
            sum += q[i][3]
        }
    }
    return sum
}

const ss = []
const s = []
let v = 0
while (v !== undefined) {
    if (s.length === N) {
        ss.push([...s])
        s.pop()
        v++
    } else if (v >= M) {
        v = s.pop()
    } else {
        s.push(v + 1)
    }
}

let A = null
let ans = 0
while (A = ss.pop()) {
   ans = Math.max(ans, f(A)) 
}

console.log(ans)
