let nl = readLine()!.split(separator: " ").map { Int($0)! }
let n = nl[0], m = nl[1], q = nl[2]
var a = [Int](repeating: 0, count: q)
var b = [Int](repeating: 0, count: q)
var c = [Int](repeating: 0, count: q)
var d = [Int](repeating: 0, count: q)
var maybeMax = 0
for i in 0..<q {
  let s = readLine()!.split(separator: " ").map { Int($0)! }
  a[i] = s[0]; b[i] = s[1]; c[i] = s[2]; d[i] = s[3]
}

func solve(f: [Int], n: Int, range: Range<Int>) {
  if f.count != n {
    for i in range {
      solve(f: f + [i], n: n, range: i..<range.max()! + 1)
    }
  } else {
    var t = 0
    for i in 0..<q {
      if f[b[i] - 1] - f[a[i] - 1] == c[i] {
        t += d[i]
      }
    }
    maybeMax = max(t, maybeMax)
  }
}
solve(f: [], n: n, range: 1..<m+1)
print(maybeMax)