"use strict";

function main(input) {
    const parsedInput = parser(input);
    mainCalc(parsedInput);
}

function parser(inputStr) {
    const splitInput = inputStr.split('\n');
    const head = splitInput[0].split(' ');
    splitInput.shift();
    const rows = splitInput.map(value => {
        const input = value.split(' ');
        return {
           a: Number(input[0]),
           b: Number(input[1]),
           c: Number(input[2]),
           d: Number(input[3])
        };
    });
    return {
        n: Number(head[0]),
        m: Number(head[1]),
        q: Number(head[2]),
        abcdList: rows,
    };
}
const mainCalc = ({n, m, q, abcdList}) => {
    let maxNum = 0; 
    const abcdCalc = (A) => {
        return abcdList
            .map(abcd => {
                if (A[abcd.b - 1] - A[abcd.a - 1] === abcd.c) {
                    return abcd.d
                }
                return 0;
            })
            .reduce((prev, next) => prev + next);
    }

    const getPatterns = (i, patterns) => {
        if (i > n - 1) {
            const result = abcdCalc(patterns, abcdList);
            maxNum = Math.max(result, maxNum);
            return;
        }
        for (let j = 0; j < m; j++ ) {
            patterns[i] = j;
            getPatterns(i + 1, patterns);
        }
    };
    getPatterns(0, new Array(n).fill(0));
    console.log(maxNum);
}

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