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

data class Quartet<out A, out B, out C, out D>(val first: A, val second: B, val third: C, val fourth: D)

fun main(args: Array<String>) {
  val (_, m, q) = readLine()!!.split(' ').map(String::toInt)
  val rules = Array(q) {
    val (a, b, c, d) = readLine()!!.split(' ').map(String::toInt)
    Quartet(a - 1, b - 1, c, d)
  }
  var ans = 0
  for (a0 in 1..m)
    for (a1 in a0..m)
      for (a2 in a1..m)
        for (a3 in a2..m)
          for (a4 in a3..m)
            for (a5 in a4..m)
              for (a6 in a5..m)
                for (a7 in a6..m)
                  for (a8 in a7..m)
                    for (a9 in a8..m) {
                      val a = intArrayOf(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
                      var sc = 0
                      for (r in rules) {
                        if (a[r.second] - a[r.first] == r.third) {
                          sc += r.fourth
                        }
                      }
                      ans = max(ans, sc)
                    }
  println(ans)
}
