"use strict";
var input=require("fs").readFileSync("/dev/stdin","utf8");
var cin=input.split(/ |\n/),cid=0;
function next(a){return a?cin[cid++]:+cin[cid++];}
function nexts(n,a){return a?cin.slice(cid,cid+=n):cin.slice(cid,cid+=n).map(a=>+a);}
function nextm(h,w,a){var r=[],i=0;if(a)for(;i<h;i++)r.push(cin.slice(cid,cid+=w));else for(;i<h;i++)r.push(cin.slice(cid,cid+=w).map(a=>+a));return r;}
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill().map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill().map(x=>"+r+")";return eval(r);}

var myOut = main();
if(myOut !== undefined)console.log(myOut);

function main(){
  var [n,m,q] = nexts(3);
  var d = nextm(q,4);
  var a = xArray(1,n);
  var ans = 0;
  while(1){
    var sum = 0;
    for(var i = 0; i < q; i++){
      var [w,x,y,z] = d[i]; 
      if(a[x-1]-a[w-1] === y)sum += z;
    }
    if(ans < sum)ans = sum;
    var f = 1;
    for(var i = n; i-- && f;){
      if(a[i] !== m){
        f = 0, a[i]++;
        for(var j = i+1; j < n; j++)a[j] = a[i];
      }
    }
    if(f)break;
  }
  return ans;
}