import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    //nextLineとnextを同時に使ったら死ぬ
    val sc = new Scanner(System.in)

    val n, m, q = sc.nextInt()

    val ar = bfs((1 to m).map(a => Vector(a)).toVector, m, n)

    val nums = Vector.fill(q) {
      val a, b, c = sc.nextInt()
      val d = sc.nextLong()
      (a, b, c, d)
    }

    val ans =
      ar.map(list => {
        nums.map {
          case (a, b, c, d) =>
            if (list(b - 1) - list(a - 1) == c) d else 0
        }.sum
      })

    println(ans.max)
  }

  @scala.annotation.tailrec
  def bfs(arr: Vector[Vector[Int]], m: Int, n: Int): Vector[Vector[Int]] = {
    if (arr.head.length == n) {
      arr
    } else {
      bfs(arr.flatMap(ar => {
        for (i <- ar.last to m) yield {
          ar :+ i
        }
      }), m, n)
    }
  }

  @scala.annotation.tailrec
  def gcd(a: Int, b: Int): Int = {
    if (b == 0) a else gcd(b, a % b)
  }
  def debug(args: Any*): Unit = {
    Console.err.println(args.mkString(" "))
  }
}
