#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
//const int INF = 2e9;
//const ll INF = 9e18;


int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, M, Q;
    cin >> N >> M >> Q;
    vector<int> a(Q),b(Q),c(Q),d(Q);
    for (int i=0;i<Q;i++){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
        a[i]--;b[i]--;
    }
    vector<int> A(N,1);
    int ans = 0;
    if (N==2){
        for (A[0]=1;A[0]<=M;A[0]++){
            for (A[1]=A[0];A[1]<=M;A[1]++){
                int sum = 0;
                for (int i=0;i<Q;i++){
                    if (A[b[i]]-A[a[i]] == c[i]) sum += d[i];
                }
                ans = max(ans,sum);
            }
        }
    } else if (N==3){
        for (A[0]=1;A[0]<=M;A[0]++){
            for (A[1]=A[0];A[1]<=M;A[1]++){
                for (A[2]=A[1];A[2]<=M;A[2]++){
                    int sum = 0;
                    for (int i=0;i<Q;i++){
                        if (A[b[i]]-A[a[i]] == c[i]) sum += d[i];
                    }
                    ans = max(ans,sum);
                }
            }
        }
    } else if (N==4){
        for (A[0]=1;A[0]<=M;A[0]++){
            for (A[1]=A[0];A[1]<=M;A[1]++){
                for (A[2]=A[1];A[2]<=M;A[2]++){
                    for (A[3]=A[2];A[3]<=M;A[3]++){
                        int sum = 0;
                        for (int i=0;i<Q;i++){
                            if (A[b[i]]-A[a[i]] == c[i]) sum += d[i];
                        }
                        ans = max(ans,sum);
                    }
                }
            }
        }
    } else if (N==5){
        for (A[0]=1;A[0]<=M;A[0]++){
            for (A[1]=A[0];A[1]<=M;A[1]++){
                for (A[2]=A[1];A[2]<=M;A[2]++){
                    for (A[3]=A[2];A[3]<=M;A[3]++){
                        for (A[4]=A[3];A[4]<=M;A[4]++){
                            int sum = 0;
                            for (int i=0;i<Q;i++){
                                if (A[b[i]]-A[a[i]] == c[i]) sum += d[i];
                            }
                            ans = max(ans,sum);
                        }
                    }
                }
            }
        }
    } else if (N==6){
        for (A[0]=1;A[0]<=M;A[0]++){
            for (A[1]=A[0];A[1]<=M;A[1]++){
                for (A[2]=A[1];A[2]<=M;A[2]++){
                    for (A[3]=A[2];A[3]<=M;A[3]++){
                        for (A[4]=A[3];A[4]<=M;A[4]++){
                            for (A[5]=A[4];A[5]<=M;A[5]++){
                                int sum = 0;
                                for (int i=0;i<Q;i++){
                                    if (A[b[i]]-A[a[i]] == c[i]) sum += d[i];
                                }
                                ans = max(ans,sum);
                            }
                        }
                    }
                }
            }
        }
    } else if (N==7){
        for (A[0]=1;A[0]<=M;A[0]++){
            for (A[1]=A[0];A[1]<=M;A[1]++){
                for (A[2]=A[1];A[2]<=M;A[2]++){
                    for (A[3]=A[2];A[3]<=M;A[3]++){
                        for (A[4]=A[3];A[4]<=M;A[4]++){
                            for (A[5]=A[4];A[5]<=M;A[5]++){
                                for (A[6]=A[5];A[6]<=M;A[6]++){
                                    int sum = 0;
                                    for (int i=0;i<Q;i++){
                                        if (A[b[i]]-A[a[i]] == c[i]) sum += d[i];
                                    }
                                    ans = max(ans,sum);
                                }
                            }
                        }
                    }
                }
            }
        }
    } else if (N==8){
        for (A[0]=1;A[0]<=M;A[0]++){
            for (A[1]=A[0];A[1]<=M;A[1]++){
                for (A[2]=A[1];A[2]<=M;A[2]++){
                    for (A[3]=A[2];A[3]<=M;A[3]++){
                        for (A[4]=A[3];A[4]<=M;A[4]++){
                            for (A[5]=A[4];A[5]<=M;A[5]++){
                                for (A[6]=A[5];A[6]<=M;A[6]++){
                                    for (A[7]=A[6];A[7]<=M;A[7]++){
                                        int sum = 0;
                                        for (int i=0;i<Q;i++){
                                            if (A[b[i]]-A[a[i]] == c[i]) sum += d[i];
                                        }
                                        ans = max(ans,sum);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } else if (N==9){
        for (A[0]=1;A[0]<=M;A[0]++){
            for (A[1]=A[0];A[1]<=M;A[1]++){
                for (A[2]=A[1];A[2]<=M;A[2]++){
                    for (A[3]=A[2];A[3]<=M;A[3]++){
                        for (A[4]=A[3];A[4]<=M;A[4]++){
                            for (A[5]=A[4];A[5]<=M;A[5]++){
                                for (A[6]=A[5];A[6]<=M;A[6]++){
                                    for (A[7]=A[6];A[7]<=M;A[7]++){
                                        for (A[8]=A[7];A[8]<=M;A[8]++){
                                            int sum = 0;
                                            for (int i=0;i<Q;i++){
                                                if (A[b[i]]-A[a[i]] == c[i]) sum += d[i];
                                            }
                                            ans = max(ans,sum);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } else if (N==10){
        for (A[0]=1;A[0]<=M;A[0]++){
            for (A[1]=A[0];A[1]<=M;A[1]++){
                for (A[2]=A[1];A[2]<=M;A[2]++){
                    for (A[3]=A[2];A[3]<=M;A[3]++){
                        for (A[4]=A[3];A[4]<=M;A[4]++){
                            for (A[5]=A[4];A[5]<=M;A[5]++){
                                for (A[6]=A[5];A[6]<=M;A[6]++){
                                    for (A[7]=A[6];A[7]<=M;A[7]++){
                                        for (A[8]=A[7];A[8]<=M;A[8]++){
                                            for (A[9]=A[8];A[9]<=M;A[9]++){
                                                int sum = 0;
                                                for (int i=0;i<Q;i++){
                                                    if (A[b[i]]-A[a[i]] == c[i]) sum += d[i];
                                                }
                                                ans = max(ans,sum);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout << ans << "\n";
}

Measured memory_bytes: 3660