import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val n, m, q = sc.nextInt()
    val qs = Vector.fill(q)((sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()))

    val a = (1 to 4).flatMap(_ => 1 to m).combinations(n).map(_.sorted)


    def f(v: Seq[Int]): Long = {
      (for {
        (a, b, c, d) <- qs
        if v(b - 1) - v(a - 1) == c
      } yield d.toLong).sum
    }

    val res = a.map(f)
    println(res.max)

  }
}
