import java.util.Scanner

import scala.annotation.tailrec
import scala.io.StdIn._

object Main extends App {

  val Array(n,m,q) = readLine().split(" ").map(_.toInt)
  val Qs = Array.fill(q) {
    val Array(a,b,c,d) = readLine().split(" ").map(_.toInt)
    Item(a-1,b-1,c,d)
  }
  case class Item(a: Int, b: Int, c: Int, d: Int)

  var ans = 0
  def solve() = {
    dfs(Seq.empty, 0, 0)
    ans
  }

  def dfs(A: Seq[Int], lastNum: Int, len: Int): Unit = {
    if(lastNum <= m) {
      if(len == n) {
        // calc score
        println(A.mkString(", "))
        val score = Qs.foldLeft(0) { case (acc, Item(a,b,c,d)) =>
          if(A(b) - A(a) == c) acc + d else acc
        }
        ans = Math.max(score, ans)
      } else {
        for (i <- 0 to m) {
          dfs(A :+ i, i, len + 1)
        }
      }
    }
  }

  println(solve())

}

