let nmq = readLine()!.split(separator: " ").map { Int($0)! }
let (n, m, q) = (nmq[0], nmq[1], nmq[2])
var a = Array(repeating: 0, count: q)
var b = Array(repeating: 0, count: q)
var c = Array(repeating: 0, count: q)
var d = Array(repeating: 0, count: q)
(0..<q).forEach { i in
  let abcd = readLine()!.split(separator: " ").map { Int($0)! }
  a[i] = abcd[0] - 1
  b[i] = abcd[1] - 1
  c[i] = abcd[2]
  d[i] = abcd[3]
}

func combinations<T: Collection>(_ collection: T, _ length: Int) -> [[T.Element]]? {
  let collection = Array(collection)
  let count = collection.count
  if count < length { return nil }
  var dict = [Int: [Int: [[T.Element]]]]()
  func r(_ index: Int, _ length: Int) -> [[T.Element]] {
    if let result = dict[index]?[length] { return result }
    if length == 0 || index + length > count { return [[]] }
    let result = (index...(count-length)).flatMap { i in
      r(i + 1, length - 1).map { item in [collection[i]] + item }
    }
    dict[index] = dict[index] ?? [Int: [[T.Element]]]()
    dict[index]?[length] = result
    return result
  }
  return r(0, length)
}

let result = combinations(1...m+n-1, n)?.map { (preS: [Int]) -> Int in
  let s = preS.enumerated().map { (i, element) in element - i }
  return (0..<q).filter {
    s[b[$0]] - s[a[$0]] == c[$0]
  }.map { d[$0] }.reduce(0, +)
}.max() ?? -1

print(result)
