function Main(input) {
  input = input.split('\n');
  let temp = [];
  for (let i in input) {
    temp.push(input[i].split(' '))
  }

  const N = parseInt(temp[0][0])
  const M = parseInt(temp[0][1])
  const Q = parseInt(temp[0][2])

  temp.shift();

  let [a, b, c, d] = [[], [], [], []];
  for (let i in temp) {
    [a[i], b[i], c[i], d[i]] = [parseInt(temp[i][0]), parseInt(temp[i][1]), parseInt(temp[i][2]), parseInt(temp[i][3])]
  }

  let A = [];

  function recursive(current){
    if (current.length >= N){
      A.push(current);
      return;
    }
    for (let i = 1; i <= M; i++){
      if(!current.length){
        recursive([i]);
      } else if (current[current.length - 1] <= i){
        recursive([...current, i])
      }
    }
  }

  recursive([]);

  let max = 0; // Ab - Aa = c
  for (let value of A){
    let sum = 0;
    for (let i = 0; i < Q; i++) {
      if (value[b[i] - 1] - value[a[i]- 1] === c[i]){
        sum += d[i]
      }
    }
    if (max <= sum){
      max = sum;
    }
  }

  console.log(max)
}

// Main('3 4 3\n1 3 3 100\n1 2 2 10\n2 3 2 10');

// Don't edit this line!
Main(require("fs").readFileSync("/dev/stdin", "utf8"));