fun main(args: Array<String>) {
  val (N, M, Q) = readInts()
  val ABCD = (1..Q).map { readInts() }

  val array = Array(N) { 0 }

  var max = 0
  fun f(): Int {
//    println(array.joinToString())
    val sum = ABCD.sumBy {
      val (a, b, c, d) = it
      if (array[b - 1] - array[a - 1] == c) d else 0
    }
//    println(sum)
    return sum
  }

  fun dfs(i: Int, min: Int) {
    for (m in min..M) {
      array[i] = m
      if (i + 1 == N)
        max = Math.max(max, f())
      else
        dfs(i + 1, m)
    }
  }
  dfs(0, 1)

  println(max)
}

fun readString() = readLine()!!
fun readStrings() = readString().split(" ")
fun readInt() = readString().toInt()
fun readLong() = readString().toLong()
fun readInts() = readStrings().map { it.toInt() }
fun readLongs() = readStrings().map { it.toLong() }
