#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <tuple>
#include <cmath>
#include <iomanip>
#include <map>
#include <stdio.h>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional> //operator[]
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define Rep(i,n) for(int i=1;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define Rrep(i,n) for(int i=n-1;i>0;i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(),(a).end()
#define P pair<int,int>
#define Pll pair<ll,ll>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int main(){
    int n,m,q; cin >> n >> m >> q;
    int a[q],b[q],c[q],d[q];
    rep(i,q) cin >> a[i] >> b[i] >> c[i] >> d[i];
    ll ans=-10000;
    for(int a1=1;a1<=10;a1++){
        for(int a2=a1;a2<=10;a2++){
            for(int a3=a2;a3<=10;a3++){
                for(int a4=a3;a4<=10;a4++){
                    for(int a5=a4;a5<=10;a5++){
                        for(int a6=a5;a6<=10;a6++){
                            for(int a7=a6;a7<=10;a7++){
                                for(int a8=a7;a8<=10;a8++){
                                    for(int a9=a8;a9<=10;a9++){
                                        for(int a10=a9;a10<=10;a10++){
                                            if(n==1&&a1>m) continue;
                                            if(n==2&&a2>m) continue;
                                            if(n==3&&a3>m) continue;
                                            if(n==4&&a4>m) continue;
                                            if(n==5&&a5>m) continue;
                                            if(n==6&&a6>m) continue;
                                            if(n==7&&a7>m) continue;
                                            if(n==8&&a8>m) continue;
                                            if(n==9&&a9>m) continue;
                                            if(n==10&&a10>m) continue;
                                            ll res=0;
                                            rep(i,q){
                                                int x,y;
                                                if(a[i]==1) x=a1;
                                                if(a[i]==2) x=a2;
                                                if(a[i]==3) x=a3;
                                                if(a[i]==4) x=a4;
                                                if(a[i]==5) x=a5;
                                                if(a[i]==6) x=a6;
                                                if(a[i]==7) x=a7;
                                                if(a[i]==8) x=a8;
                                                if(a[i]==9) x=a9;
                                                if(a[i]==10) x=a10;
                                                if(b[i]==1) y=a1;
                                                if(b[i]==2) y=a2;
                                                if(b[i]==3) y=a3;
                                                if(b[i]==4) y=a4;
                                                if(b[i]==5) y=a5;
                                                if(b[i]==6) y=a6;
                                                if(b[i]==7) y=a7;
                                                if(b[i]==8) y=a8;
                                                if(b[i]==9) y=a9;
                                                if(b[i]==10) y=a10;
                                                if(y-x==c[i]) res+=d[i];
                                            }
                                            chmax(ans,res);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout << ans << endl;
}



Measured memory_bytes: 3600