function Main(input) {
	input = input.trim().split("\n").map(function(x) { return x.split(" ")});
	var N = parseInt(input[0][0]);
	var M = parseInt(input[0][1]);
	var Q = parseInt(input[0][2]);
	var ary_abcd = [];
	var tmp = [];
  	var x_max = 0;
	var countX_max = 0;
	var tradeOffList = [];
	var q01ary = [];
	var d = 0;
	var tmpD = 0;
	var vec_A = Array.from({ length: N }, () => undefined); 
	var tmpA;
	for (var i = 1;  i <= Q; i++){
		ary_abcd.push(input[i].map(e => parseInt(e, 10)));
	}
//   console.log( "here");
	for (var i = 1; i < N; i++){
		for (var j = 2; j <= N; j++){
	        countX_max = 0;
			for (var q = 0; q < Q; q++){
				if (ary_abcd[q][0] == i && ary_abcd[q][1] == j){
					tmp.push(ary_abcd[q].slice());
             	    countX_max ++;
             	    if (countX_max === 2){
             	    	tradeOffList.push([tmp.length - 2, tmp.length - 1])
             	    } else if (countX_max === 3){
             	    	tradeOffList[tradeOffList.length - 1].push(tmp.length - 1)
             	    }
				}
			}
          	x_max += Math.min(1, countX_max);
		}
	}
	ary_abcd = tmp.slice(); //ai bi ci diの配列をソート
	
	//数が大きい方から調べる x >= N
	for (var x = N; x <= x_max; x++){
    //   console.log("x",x);
		q01ary = make01Ary(Q, (Q-x), tradeOffList);	
		for (var i = 0; i < q01ary.length; i++){
//		console.log(q01ary.length);
  //              console.log(q01ary[i]);
			tmpD = 0;
          each01check_loop:
			for (var j = 0; j < Q; j++){
                    if (q01ary[i][j] === 0) continue;
                    // console.log("here", q01ary[i][j], j)
					if(vec_A[ary_abcd[j][0] - 1] === undefined && vec_A[ary_abcd[j][1] - 1] === undefined){
						vec_A[ary_abcd[j][0] - 1] = 0;
                        vec_A[ary_abcd[j][1] - 1] = ary_abcd[j][2];
					} else if (vec_A[ary_abcd[j][0] - 1] === undefined) {
						vec_A[ary_abcd[j][0] - 1] = vec_A[ary_abcd[j][1] - 1] - ary_abcd[j][2];
					} else if (vec_A[ary_abcd[j][1] - 1] === undefined) {
						vec_A[ary_abcd[j][1] - 1] = vec_A[ary_abcd[j][0] - 1] + ary_abcd[j][2];
					} else {
                        if (vec_A[ary_abcd[j][1] - 1] !== vec_A[ary_abcd[j][0] - 1] + ary_abcd[j][2]) { //連立方程式解なし
                            tmpD = 0;
                            // console.log("break");
                            break each01check_loop;	
						} 
                    }
					tmpD +=  	ary_abcd[j][3];
            }
			tmpA = vec_A.filter( function( value ) {
    			return value !== undefined;
                })
            vec_A = Array.from({ length: N }, () => undefined);     
            // console.log(tmpD);
			if ((Math.max.apply(tmpA) - Math.min.apply(tmpA)) < 10 ) {
				d = Math.max(d, tmpD);
// console.log(d, q01ary);
			}
		}
    }
    
    //数が小さい方へx < N
    for (var x = (N - 1); x > 0; x--){
            // console.log("x",x);
        q01ary = make01Ary(Q, (Q-x), tradeOffList);	
            // console.log(q01ary);
		for (var i = 0; i < q01ary.length; i++){
            // console.log(q01ary[i]);
  //              console.log(q01ary[i]);
			tmpD = 0;
          	each01check_loop:
			for (var j = 0; j < Q; j++){
                    // $(vec_A);
                    // $("???");
                    // $(q01ary);
                    // $(i);
                    // $(j);
                    // $(ary_abcd[j]);
                    // console.log(vec_A[ary_abcd[j][0]-1]);
                    // console.log(vec_A[ary_abcd[j][1]-1]);
					if (q01ary[i][j] === 0) continue;
					if(vec_A[ary_abcd[j][0] - 1] === undefined && vec_A[ary_abcd[j][1] - 1] === undefined){
						vec_A[ary_abcd[j][0] - 1] = 0;
                        vec_A[ary_abcd[j][1] - 1] = ary_abcd[j][2];
                        // $(0);
					} else if (vec_A[ary_abcd[j][0] - 1] === undefined) {
                        vec_A[ary_abcd[j][0] - 1] = vec_A[ary_abcd[j][1] - 1] - ary_abcd[j][2];
                        // $(1);
					} else if (vec_A[ary_abcd[j][1] - 1] === undefined) {
                        vec_A[ary_abcd[j][1] - 1] = vec_A[ary_abcd[j][0] - 1] + ary_abcd[j][2];
                        // $(2);
					} else {
                        if (vec_A[ary_abcd[j][1] - 1] !== (vec_A[ary_abcd[j][0] - 1] + ary_abcd[j][2])) { //連立方程式解なし
                            tmpD = 0;
                            // $(3);
                            // $(vec_A);
                            // $(vec_A[ary_abcd[j][1] - 1]);
                            // $(vec_A[ary_abcd[j][0] - 1] + ary_abcd[j][2]);
                            // console.log("break");
							break each01check_loop;	
						} 
					}
					tmpD += ary_abcd[j][3];
            }
            // console.log(tmpD);
			tmpA = vec_A.filter( function( value ) {
    			return value !== undefined;
                })
            vec_A = Array.from({ length: N }, () => undefined);     
			if ((Math.max.apply(tmpA) - Math.min.apply(tmpA)) < 10 ) {
				d = Math.max(d, tmpD);
                            // console.log(d, q01ary[j]);
			}
		}
	}
	console.log(d);  
}   


function compareFunc(a, b) {
	return a - b;
}


Main(require("fs").readFileSync("/dev/stdin", "utf8"));


function make01Ary(whole, zero, skip){
	var zeros = Array.from({ length: zero }, () => 0); 
	var ones =  Array.from({ length: (whole - zero) }, () => 1); 
	var v = zeros.concat(ones);
	var ary = []; //配列を入れる配列
  	ary.push(v.slice());
	var tmp;
	var overlapped = 0;
    for (var i = 1; i < (kaijo(whole)/(kaijo(zero)*kaijo(whole-zero))); i++) {
        switch_loop:
        for (var j = 2; j <= whole ; j++){
            for (var k = 1; k < j; k++){
                if (v[whole - j] < v[whole - k]){
                    tmp = v[whole - j];
                    v[whole - j] = v[whole-k];
                    v[whole - k] = tmp;
                    v = v.slice(0, (whole - j + 1)).concat(v.slice(whole - j + 1).sort(compareFunc));
                	for (var ii = 0; ii < skip.length; ii ++){
						overlapped = 0;
                		for (var ij = 0; ij < skip[ii].length; ij++){
                			overlapped += v[skip[ii][ij]];
                            if (overlapped > 1) {
	                      //console.log("here", i);
   		                  break switch_loop;
                  		  }
                		}
                	}
//                  	console.log(overlapped, v);
                    ary.push(v.slice());
                    break switch_loop; 
                }
            }
        }
    }
    return(ary);
}   

function kaijo(num){
    var ans = 1;
    for (var i = 2; i <= num; i++){
        ans *= i;
    } 
    return ans;    
}


function $(text, tmpStyle){
    var str = String(text);
    tmpStyle = (typeof  tmpStyle !== 'undefined') ?  tmpStyle : 0
    if (tmpStyle ===0){
        console.log("////", str);
    }
    if (tmpStyle ===1){
        var l = str.length;
        var lines = Array (l + 1 + 10).join('#');
        var spaces = Array (5).join(' ');
        console.log( lines);
        console.log( "%s%s%s%s%s", '#', spaces, str, spaces, '#');
        console.log( lines);
    }
}   


