import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;

void main() {
	auto NMQ = readln.split.to!(int[]);
	auto N = NMQ[0], M = NMQ[1], Q = NMQ[2];
	int[4][] abcd;
	foreach (i; 0 .. Q) {
	    abcd ~= readln.split[0 .. 4].to!(int[4]);
	}
	int ans;
	foreach (A; A_s(N, M)) {
	    int tmp;
	    foreach (i; 0 .. Q) {
	        if (A[abcd[i][1]-1] - A[abcd[i][0]-1] == abcd[i][2]) tmp += abcd[i][3];
	    }
	    ans = max(ans, tmp);
	}
	writeln(ans);
}

int[][] A_s(int N, int M) {
    if (N == 1) {
        return [[1]];
    }
    auto Alist = A_s(N-1, M);
    int[][] result;
    
    foreach (As; Alist) {
        foreach (last; As[$-1] .. M+1) {
            result ~= (As~[last]);
        }
    }
    
    return result;
}