#include <bits/stdc++.h>

//#define DEBUG

using namespace std;

int MAX_DEPTH = 10;
int MIN_DIGIT = 1;
int MAX_DIGIT = 9;
int count_malloc = 0;
int count_delete = 0;

template <class T>
void print_vect(std::vector<T> v){
    for(int i = 0 ; i < v .size(); i++) cout << v[i] << " ";
    cout << endl;
}

struct my_tree{
    int val;
    int kid_num;
    my_tree* kids[10];
};

struct condition{
    int a;
    int b;
    int c;
    uint64_t d;
};


void delete_tree(my_tree* root, int current_depth){
    if( current_depth == MAX_DEPTH){
        //count_delete++;
        delete(root);
        return;
    }
    for(int i = 0 ; i < root->kid_num ; i++){
        delete_tree(root->kids[i], current_depth + 1);
    }
    delete(root);
    //count_delete++;
}

void inflate_tree(my_tree* root, int current_depth, int MAX_DEPTH, std::vector<int>& path, 
                std::vector<condition>& requirements, uint64_t& max_score){
    if(current_depth <= MAX_DEPTH){
        root->kid_num = MAX_DIGIT - root->val + 1;
        //cout << "kid num " << root->kid_num << endl;
        for(int i = 0 ; i < root->kid_num; i++){
            root->kids[i] = new my_tree;
            //count_malloc++;
            root->kids[i]->val = root->val + i;
            path[current_depth] = root->kids[i]->val;
            inflate_tree(root->kids[i], current_depth + 1, MAX_DEPTH, path, requirements, max_score);
        }
    }else{
        //print_vect(path);
        uint64_t score = 0;
        for(int q = 0 ; q < requirements.size() ; q++){
            int a = requirements[q].a;
            int b = requirements[q].b;
            int c = requirements[q].c;
            uint64_t d = requirements[q].d;
            if(path[b] - path[a] == c){
                score += d;
            }
        }
        if( score > max_score){
            max_score = score;
        }
        //cout << "current max " << max_score << endl;
    } 
}

void depth_first_search(){
    int N, M, Q;
    cin >> N >> M >> Q;
    MAX_DIGIT = M;
    MAX_DEPTH = N;
    std::vector<condition> requirements;
    for(int i = 0 ; i < Q ; i++){
        int a, b, c;
        uint64_t d;
        cin >> a >> b >> c >> d;
        condition cond;
        cond.a = a;
        cond.b = b;
        cond.c = c;
        cond.d = d;
        requirements.push_back(cond);
    }
    my_tree* root = new my_tree;
    //count_malloc++;
    root->val = MIN_DIGIT;
    root->kid_num = MAX_DIGIT - MIN_DIGIT + 1;
    uint64_t max_score = 0;
    std::vector<int> graph_path(MAX_DEPTH + 1, 0);
    for(int i = 0 ; i < root->kid_num; i++){
        root->kids[i] = new my_tree;
        //count_malloc++;
        root->kids[i]->val = root->val + i;
        graph_path[1] = root->kids[i]->val;
        inflate_tree(root->kids[i], 2, MAX_DEPTH, graph_path, requirements, max_score);
    }
    delete_tree(root, 0);
    cout << max_score << endl;
}


void func_to_create(){
    depth_first_search();
    //cout << "malloc " << count_malloc << endl;
    //cout << "delete " << count_delete << endl;
}
int main()
{
    //making data IO Fast
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    /****************************/

    #ifdef DEBUG
    int TEST_CASES = 3;
    while(TEST_CASES--){
    #endif // DEBBUG

    func_to_create();

    #ifdef DEBUG
    }
    #endif // DEBUG
    
    return 0;
}


Measured memory_bytes: 20996