#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define pb push_back
#define sz(x) (int)(x).size()
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int mod = 1000000007;


int main(){
    int ans = 0;
    int n, m, q;
    cin >> n >> m >> q;

    vvi req(q, vi(4));
    rep(i, q)rep(j, 4){
        cin >> req[i][j];
    }

    int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;

    vi seq(n);

    if(n==10){
    for(a=1;a<=m;a++){
        seq[0]=a;
        for(b=a;b<=m;b++){
            seq[1]=b;
            for(c=b;c<=m;c++){
                seq[2]=c;
                for(d=c;d<=m;d++){
                    seq[3]=d;
                    for(e=d;e<=m;e++){
                        seq[4]=e;
                        for(f=e;f<=m;f++){
                            seq[5]=f;
                            for(g=f;g<=m;g++){
                                seq[6]=g;
                                for(h=g;h<=m;h++){
                                    seq[7]=h;
                                    for(i=h;i<=m;i++){
                                        seq[8]=i;
                                        for(j=i;j<=m;j++){
                                            seq[9]=j;
                                            
                                            int tmp = 0;
                                            rep(x, q){
                                                if(seq[req[x][1]-1] - seq[req[x][0]-1] == req[x][2]){
                                                    tmp += req[x][3];
                                                }
                                            }

                                            ans = max(ans, tmp);

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    }

    if(n==9){
    for(a=1;a<=m;a++){
        seq[0]=a;
        for(b=a;b<=m;b++){
            seq[1]=b;
            for(c=b;c<=m;c++){
                seq[2]=c;
                for(d=c;d<=m;d++){
                    seq[3]=d;
                    for(e=d;e<=m;e++){
                        seq[4]=e;
                        for(f=e;f<=m;f++){
                            seq[5]=f;
                            for(g=f;g<=m;g++){
                                seq[6]=g;
                                for(h=g;h<=m;h++){
                                    seq[7]=h;
                                    for(i=h;i<=m;i++){
                                        seq[8]=i;
                                        
                                            
                                        int tmp = 0;
                                        rep(x, q){
                                            if(seq[req[x][1]-1] - seq[req[x][0]-1] == req[x][2]){
                                                tmp += req[x][3];
                                            }
                                        }

                                        ans = max(ans, tmp);

                                        
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    }

    if(n==8){
    for(a=1;a<=m;a++){
        seq[0]=a;
        for(b=a;b<=m;b++){
            seq[1]=b;
            for(c=b;c<=m;c++){
                seq[2]=c;
                for(d=c;d<=m;d++){
                    seq[3]=d;
                    for(e=d;e<=m;e++){
                        seq[4]=e;
                        for(f=e;f<=m;f++){
                            seq[5]=f;
                            for(g=f;g<=m;g++){
                                seq[6]=g;
                                for(h=g;h<=m;h++){
                                    seq[7]=h;
                                    
                                        
                                            
                                    int tmp = 0;
                                    rep(x, q){
                                        if(seq[req[x][1]-1] - seq[req[x][0]-1] == req[x][2]){
                                            tmp += req[x][3];
                                        }
                                    }

                                    ans = max(ans, tmp);

                                        
                                    
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    }

    if(n==7){
    for(a=1;a<=m;a++){
        seq[0]=a;
        for(b=a;b<=m;b++){
            seq[1]=b;
            for(c=b;c<=m;c++){
                seq[2]=c;
                for(d=c;d<=m;d++){
                    seq[3]=d;
                    for(e=d;e<=m;e++){
                        seq[4]=e;
                        for(f=e;f<=m;f++){
                            seq[5]=f;
                            for(g=f;g<=m;g++){
                                seq[6]=g;
                                
                                    
                                        
                                            
                                int tmp = 0;
                                rep(x, q){
                                    if(seq[req[x][1]-1] - seq[req[x][0]-1] == req[x][2]){
                                        tmp += req[x][3];
                                    }
                                }

                                ans = max(ans, tmp);

                                        
                                    
                                
                            }
                        }
                    }
                }
            }
        }
    }
    }

    if(n==6){
    for(a=1;a<=m;a++){
        seq[0]=a;
        for(b=a;b<=m;b++){
            seq[1]=b;
            for(c=b;c<=m;c++){
                seq[2]=c;
                for(d=c;d<=m;d++){
                    seq[3]=d;
                    for(e=d;e<=m;e++){
                        seq[4]=e;
                        for(f=e;f<=m;f++){
                            seq[5]=f;
                            
                                
                                    
                                        
                                            
                            int tmp = 0;
                            rep(x, q){
                                if(seq[req[x][1]-1] - seq[req[x][0]-1] == req[x][2]){
                                    tmp += req[x][3];
                                }
                            }

                            ans = max(ans, tmp);

                                        
                                    
                                
                            
                        }
                    }
                }
            }
        }
    }
    }

    if(n==5){
    for(a=1;a<=m;a++){
        seq[0]=a;
        for(b=a;b<=m;b++){
            seq[1]=b;
            for(c=b;c<=m;c++){
                seq[2]=c;
                for(d=c;d<=m;d++){
                    seq[3]=d;
                    for(e=d;e<=m;e++){
                        seq[4]=e;
                        
                            
                                
                                    
                                        
                                            
                        int tmp = 0;
                        rep(x, q){
                            if(seq[req[x][1]-1] - seq[req[x][0]-1] == req[x][2]){
                                tmp += req[x][3];
                            }
                        }

                        ans = max(ans, tmp);

                                        
                                    
                                
                            
                        
                    }
                }
            }
        }
    }
    }

    if(n==4){
    for(a=1;a<=m;a++){
        seq[0]=a;
        for(b=a;b<=m;b++){
            seq[1]=b;
            for(c=b;c<=m;c++){
                seq[2]=c;
                for(d=c;d<=m;d++){
                    seq[3]=d;
                    
                                            
                    int tmp = 0;
                    rep(x, q){
                        if(seq[req[x][1]-1] - seq[req[x][0]-1] == req[x][2]){
                            tmp += req[x][3];
                        }
                    }

                    ans = max(ans, tmp);

                    
                }
            }
        }
    }
    }

    if(n==3){
    for(a=1;a<=m;a++){
        seq[0]=a;
        for(b=a;b<=m;b++){
            seq[1]=b;
            for(c=b;c<=m;c++){
                seq[2]=c;
                
                    
                                            
                int tmp = 0;
                rep(x, q){
                    if(seq[req[x][1]-1] - seq[req[x][0]-1] == req[x][2]){
                        tmp += req[x][3];
                    }
                }

                ans = max(ans, tmp);

                    
                
            }
        }
    }
    }

    if(n==2){
    for(a=1;a<=m;a++){
        seq[0]=a;
        for(b=a;b<=m;b++){
            seq[1]=b;
            
                
                    
                                            
                int tmp = 0;
                rep(x, q){
                    if(seq[req[x][1]-1] - seq[req[x][0]-1] == req[x][2]){
                        tmp += req[x][3];
                    }
                }

                ans = max(ans, tmp);

                    
                
            
        }
    }
    }



    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3600