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), b = a, c = a, d = a
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(range: ClosedRange<Int>, f: [Int] = []) {
  if f.count != n {
    for i in range {
      solve(range: i...range.upperBound, f: f + [i])
    }
  } 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(range: 1...m)
print(maybeMax)