#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>

using namespace std;

int main(){

    int N, M, Q;
    cin >> N >> M >> Q;

    int mat[Q][4];
    for(int i=0;i<Q;i++){
        cin >> mat[i][0] >> mat[i][1] >> mat[i][2] >> mat[i][3];
    }

    vector<int> A;
    int score;

    int result;
    result=0;

    for(int A1=1; A1<=M; A1++){
        for(int A2=A1; A2<=M; A2++){
            if(N == 2){
                A.clear();
                A.push_back(A1);
                A.push_back(A2);

                score = 0;
                for(int i = 0; i<Q; i++){
                    if(A[mat[i][1]-1] - A[mat[i][0]-1] == mat[i][2]){
                        score += mat[i][3];
                    }
                }
                if(result < score){
                    result = score;
                }
            }
            if(N > 2){
                for(int A3=A2; A3<=M; A3++){                    
                    if(N == 3){
                        A.clear();
                        A.push_back(A1);
                        A.push_back(A2);
                        A.push_back(A3);

                        score = 0;
                        for(int i = 0; i<Q; i++){
                            if(A[mat[i][1] - 1] - A[mat[i][0] - 1] == mat[i][2]){
                                score += mat[i][3];
                            }
                        }
                        if(result < score){
                            result = score;
                        }
                    }
                    if(N > 3){
                        for(int A4=A3; A4<=M; A4++){                    
                            if(N == 4){
                                A.clear();
                                A.push_back(A1);
                                A.push_back(A2);
                                A.push_back(A3);
                                A.push_back(A4);

                        score = 0;
                        for(int i = 0; i<Q; i++){
                            if(A[mat[i][1] - 1] - A[mat[i][0] - 1] == mat[i][2]){
                                score += mat[i][3];
                            }
                        }
                        if(result < score){
                            result = score;
                        }
                            }
                            if(N > 4){
                                for(int A5=A4; A5<=M; A5++){                    
                                    if(N == 5){
                                        A.clear();
                                        A.push_back(A1);
                                        A.push_back(A2);
                                        A.push_back(A3);
                                        A.push_back(A4);
                                        A.push_back(A5);

                                                                score = 0;
                        for(int i = 0; i<Q; i++){
                            if(A[mat[i][1] - 1] - A[mat[i][0] - 1] == mat[i][2]){
                                score += mat[i][3];
                            }
                        }
                        if(result < score){
                            result = score;
                        }
                                    }
                                    if(N > 5){
                                        for(int A6=A5; A6<=M; A6++){
                                            if(N == 6){
                                                A.clear();
                                                A.push_back(A1);
                                                A.push_back(A2);
                                                A.push_back(A3);
                                                A.push_back(A4);
                                                A.push_back(A5);
                                                A.push_back(A6);

                                                                        score = 0;
                        for(int i = 0; i<Q; i++){
                            if(A[mat[i][1] - 1] - A[mat[i][0] - 1] == mat[i][2]){
                                score += mat[i][3];
                            }
                        }
                        if(result < score){
                            result = score;
                        }
                                            }
                                            if(N > 6){
                                                for(int A7=A6; A7<=M; A7++){
                                                    if(N == 7){
                                                        A.clear();
                                                        A.push_back(A1);
                                                        A.push_back(A2);
                                                        A.push_back(A3);
                                                        A.push_back(A4);
                                                        A.push_back(A5);
                                                        A.push_back(A6);
                                                        A.push_back(A7);

                                                                                score = 0;
                        for(int i = 0; i<Q; i++){
                            if(A[mat[i][1] - 1] - A[mat[i][0] - 1] == mat[i][2]){
                                score += mat[i][3];
                            }
                        }
                        if(result < score){
                            result = score;
                        }
                                                    }
                                                    if(N > 7){
                                                        for(int A8=A7; A8<=M; A8++){
                                                            if(N == 8){
                                                                A.clear();
                                                                A.push_back(A1);
                                                                A.push_back(A2);
                                                                A.push_back(A3);
                                                                A.push_back(A4);
                                                                A.push_back(A5);
                                                                A.push_back(A6);
                                                                A.push_back(A7);
                                                                A.push_back(A8);

                                                                                        score = 0;
                        for(int i = 0; i<Q; i++){
                            if(A[mat[i][1] - 1] - A[mat[i][0] - 1] == mat[i][2]){
                                score += mat[i][3];
                            }
                        }
                        if(result < score){
                            result = score;
                        }
                                                            }
                                                            if(N > 8){
                                                                for(int A9=A8; A9<=M; A9++){
                                                                    if(N == 9){
                                                                        A.clear();
                                                                        A.push_back(A1);
                                                                        A.push_back(A2);
                                                                        A.push_back(A3);
                                                                        A.push_back(A4);
                                                                        A.push_back(A5);
                                                                        A.push_back(A6);
                                                                        A.push_back(A7);
                                                                        A.push_back(A8);
                                                                        A.push_back(A9);

                                                                                                score = 0;
                        for(int i = 0; i<Q; i++){
                            if(A[mat[i][1] - 1] - A[mat[i][0] - 1] == mat[i][2]){
                                score += mat[i][3];
                            }
                        }
                        if(result < score){
                            result = score;
                        }
                                                                    }
                                                                    if(N > 9){
                                                                        for(int A10=A9; A10<=M; A10++){
                                                                            A.clear();
                                                                            A.push_back(A1);
                                                                            A.push_back(A2);
                                                                            A.push_back(A3);
                                                                            A.push_back(A4);
                                                                            A.push_back(A5);
                                                                            A.push_back(A6);
                                                                            A.push_back(A7);
                                                                            A.push_back(A8);
                                                                            A.push_back(A9);
                                                                            A.push_back(A10);

                                                                                                    score = 0;
                        for(int i = 0; i<Q; i++){
                            if(A[mat[i][1] - 1] - A[mat[i][0] - 1] == mat[i][2]){
                                score += mat[i][3];
                            }
                        }
                        if(result < score){
                            result = score;
                        }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }        
                            }
                        }
                    }
                }                
            }
        }
    }

    cout << result << endl;

    return 0;
}






Measured memory_bytes: 3180