var arg = require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n")
var [N, M, Q] = arg[0].split(" ");
var A = new Array(N).fill(0)
var arr = new Array(Q)
for(var i = 1; i <= Q; i ++) {
  arr[i - 1] = arg[i].split(" ")
}
var max = 0;
simulateAll(0, A, arr)
console.log(max)

function simulateAll(ind, A) {
  if(ind == N) {
    calcScore(A)
    return;
  } else {
     //全パターン試す
    for(var i = 1; i <= M; i ++ ) {
      A[ind] = i
      simulateAll(ind + 1, A)
    }
  }
}

function calcScore(A) {
  var score = 0;
  arr.forEach(elm => {
    if(A[elm[1] - 1] - A[elm[0] - 1] == elm[2]) {
      score += parseInt(elm[3])
    }
  })
  if(score > max) {
  	max = score
  }
}
