object Main extends App{
  val Array(n,m,q) = scala.io.StdIn.readLine.split(" ").map(_.toInt)
  val abcd = for(count <- 1 to q) yield scala.io.StdIn.readLine.split(" ").map(_.toInt)
  val As = (1 to m).combinations(n)
  val scores = for(A <- As) yield {
    abcd.filter(e => A(e(1)-1)-A(e(0)-1)==e(2)).map(_(3)).sum
  }
  println(scores.max)
}
