function main(input) {
  let [n, m, q] = input[0].trim().split(" ").map(i => parseInt(i));

  let [a,b,c,d] = [[],[],[],[]];
  for(i=0;i<q;i++){
    [a[i], b[i], c[i], d[i]] = input[i+1].trim().split(" ").map(i => parseInt(i));
  }

  let A = [];

  /*　再帰頑張った　*/
  function recursive(current) {
    if(current.length>=n){
      A.push(current)
      return;
    }
    for(let i=1;i<=m;i++){
      if(current.length===0){
        recursive([i]);
      }else if((current[current.length-1]<=i)){
        recursive([...current,i]);
      }
    }
  }

  recursive([]);
  
  let max=0;
  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(require('fs').readFileSync('/dev/stdin', 'utf8').trim().split("\n"));