function main(input) {
  var inputs = input.split('\n');

  var nums = inputs[0].split(' ');
  var n = parseInt(nums[0], 10);
  var m = parseInt(nums[1], 10);
  var q = parseInt(nums[2], 10);

  var groups = [];
  for (var i = 0; i < q; i++) {
    groups.push(inputs[i + 1].split(' ').map((n) => parseInt(n, 10)));
  }

  var source = [];
  for (var i = 1; i <= m; i++) {
    source.push(i);
  }

  // push patters to "patterns"
  for (var i = 0; i < source.length; i++) {
    genePattern(n, i, [], source);
  }

  var maxScore = 0;
  patterns.forEach((pattern, i) => {
    var score = 0;
    groups.forEach((item, i) => {
      if(pattern[item[1] - 1] - pattern[item[0] - 1] === item[2]) {
        score += item[3];
      }
    });
    if (maxScore < score) maxScore = score;
  });

  console.log(maxScore);
}

var patterns = [];
function genePattern(n, preIndex, preArray, source) {
  var temp = preArray.concat();
  temp.push(source[preIndex]);

  if (temp.length == n) {
    patterns.push(temp);
    return;
  }

  for (var i = preIndex; i < source.length; i++) {
    genePattern(n, i, temp, source);
  }
}

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