function main(input){
  input = input.split('\n');
  const [N, M, Q] = input[0].split(' ').map(Number);
  const list = input.slice(1, Q+1).map(x=> x.split(' ').map(Number));

  function createA(array) {
    let ret = []
    array.forEach(a => {
      for(let i=a[a.length-1]; i<=M; i++){
        ret.push([...a, i])
      }
    });
    if(ret[0].length === N) return ret;
    return createA(ret);
  }

  const array = []
  for(let i=1; i<=M; i++){
    array.push([i]);
  }
  const AList = createA(array)

  let max = 0;
  AList.forEach(A => {
    let sum = 0;
    list.forEach(x=>{
      if(A[x[1]-1] - A[x[0]-1] === x[2]){
        sum += x[3];
      }
    })
    if(sum > max){
      max = sum;
    }
  });

  console.log(max);
}


main(require('fs').readFileSync('/dev/stdin', 'utf8').trim());
