import java.io.{BufferedOutputStream, BufferedReader, InputStreamReader, PrintWriter}

import scala.annotation.tailrec

object Main {
  def main(args: Array[String]): Unit = {
    val in = new BufferedReader(new InputStreamReader(System.in))
    val out = new PrintWriter(new BufferedOutputStream(System.out))
    val Array(n, m, q) = in.readLine().split("\\s+").map(_.toInt)
    val queries = Array.fill(q) {
      in.readLine().split("\\s+").map(_.toInt)
    }

    val as = Array.fill(n)(1)
    var completed = false
    var cursor = 0
    var maxScore = Integer.MIN_VALUE

    @tailrec
    def next(): Boolean = {
      if(cursor < n - 1 && as(cursor) < as(cursor + 1) || cursor == n - 1 && as(cursor) < m) {
        as(cursor) += 1
        for(i <- 0 until cursor) {
          as(i) = 1
        }
        if(cursor > 0) {
          cursor -= 1
        }
        true
      } else if (cursor < n - 1) {
        cursor += 1
        next()
      } else {
        false
      }
    }

    while (!completed) {
      var score = 0
      for (query <- queries) {
        if (as(query(1) - 1) - as(query(0) - 1) == query(2)) {
          score += query(3)
        }
      }
      maxScore = Math.max(maxScore, score)

      if(!next()) {
        completed = true
      }
    }

    out.println(maxScore)
    out.flush()
  }
}
