import java.util.Scanner

import scala.collection.mutable
import scala.math._

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 m).combinations(n).map(_.sorted).toSeq

    def f(v: Seq[Int]): Int = {
      qs.map { case (a, b, c, d) =>
        if (v(b - 1) - v(a - 1) == c) d else 0
      }.sum
    }

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

  }
}
