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

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> a(q, vector<int>(4));
    for (int i = 0; i < q; ++i) {
        for (int j = 0; j < 4; ++j) {
            cin >> a[i][j];
        }
        a[i][0]--;
        a[i][1]--;
    }
    auto f = [&](vector<int> t) {
        ll cnt = 0;
        for (int i = 0; i < q; ++i) {
            if (t[a[i][1]] - t[a[i][0]] == a[i][2]) cnt += a[i][3];
        }
        return cnt;
    };
    ll ans = 0;
    vector<int> p(n);
    if (n == 2) {
        for (int i = 1; i <= m; ++i) {
            for (int j = i; j <= m; ++j) {
                p[0] = i;
                p[1] = j;
                ans = max(ans, f(p));
            }
        }
    } else if (n == 3) {
        for (int i = 1; i <= m; ++i) {
            for (int j = i; j <= m; ++j) {
                for (int k = j; k <= m; ++k) {
                    p[0] = i;
                    p[1] = j;
                    p[2] = k;
                    ans = max(ans, f(p));
                }
            }
        }
    } else if (n == 4) {
        for (int i = 1; i < m + 1; ++i) {
            for (int j = i; j < m + 1; ++j) {
                for (int k = j; k < m + 1; ++k) {
                    for (int l = k; l < m + 1; ++l) {
                        p[0] = i;
                        p[1] = j;
                        p[2] = k;
                        p[3] = l;
                        ans = max(ans, f(p));
                    }
                }
            }
        }
    } else if (n == 5) {
        for (int i = 1; i < m + 1; ++i) {
            for (int j = i; j < m + 1; ++j) {
                for (int k = j; k < m + 1; ++k) {
                    for (int l = k; l < m + 1; ++l) {
                        for (int i1 = l; i1 < m + 1; ++i1) {
                            p[0] = i;
                            p[1] = j;
                            p[2] = k;
                            p[3] = l;
                            p[4] = i1;
                            ans = max(ans, f(p));
                        }
                    }
                }
            }
        }
    } else if (n == 6) {
        for (int i = 1; i < m + 1; ++i) {
            for (int j = i; j < m + 1; ++j) {
                for (int k = j; k < m + 1; ++k) {
                    for (int l = k; l < m + 1; ++l) {
                        for (int i1 = l; i1 < m + 1; ++i1) {
                            for (int k1 = i1; k1 < m + 1; ++k1) {
                                p[0] = i;
                                p[1] = j;
                                p[2] = k;
                                p[3] = l;
                                p[4] = i1;
                                p[5] = k1;
                                ans = max(ans, f(p));
                            }
                        }
                    }
                }
            }
        }
    } else if (n == 7) {
        for (int i = 1; i < m + 1; ++i) {
            for (int j = i; j < m + 1; ++j) {
                for (int k = j; k < m + 1; ++k) {
                    for (int l = k; l < m + 1; ++l) {
                        for (int i1 = l; i1 < m + 1; ++i1) {
                            for (int k1 = i1; k1 < m + 1; ++k1) {
                                for (int l1 = k1; l1 < m + 1; ++l1) {
                                    p[0] = i;
                                    p[1] = j;
                                    p[2] = k;
                                    p[3] = l;
                                    p[4] = i1;
                                    p[5] = k1;
                                    p[6] = l1;
                                    ans = max(ans, f(p));
                                }
                            }
                        }
                    }
                }
            }
        }
    } else if (n == 8) {
        for (int i = 1; i < m + 1; ++i) {
            for (int j = i; j < m + 1; ++j) {
                for (int k = j; k < m + 1; ++k) {
                    for (int l = k; l < m + 1; ++l) {
                        for (int i1 = l; i1 < m + 1; ++i1) {
                            for (int k1 = i1; k1 < m + 1; ++k1) {
                                for (int l1 = k1; l1 < m + 1; ++l1) {
                                    for (int m1 = l1; m1 < m + 1; ++m1) {
                                        p[0] = i;
                                        p[1] = j;
                                        p[2] = k;
                                        p[3] = l;
                                        p[4] = i1;
                                        p[5] = k1;
                                        p[6] = l1;
                                        p[7] = m1;
                                        ans = max(ans, f(p));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } else if (n == 9) {
        for (int i = 1; i < m + 1; ++i) {
            for (int j = i; j < m + 1; ++j) {
                for (int k = j; k < m + 1; ++k) {
                    for (int l = k; l < m + 1; ++l) {
                        for (int i1 = l; i1 < m + 1; ++i1) {
                            for (int k1 = i1; k1 < m + 1; ++k1) {
                                for (int l1 = k1; l1 < m + 1; ++l1) {
                                    for (int m1 = l1; m1 < m + 1; ++m1) {
                                        for (int n1 = m1; n1 < m + 1; ++n1) {
                                            p[0] = i;
                                            p[1] = j;
                                            p[2] = k;
                                            p[3] = l;
                                            p[4] = i1;
                                            p[5] = k1;
                                            p[6] = l1;
                                            p[7] = m1;
                                            p[8] = n1;
                                            ans = max(ans, f(p));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } else if (n == 10) {
        for (int i = 1; i < m + 1; ++i) {
            for (int j = i; j < m + 1; ++j) {
                for (int k = j; k < m + 1; ++k) {
                    for (int l = k; l < m + 1; ++l) {
                        for (int i1 = l; i1 < m + 1; ++i1) {
                            for (int k1 = i1; k1 < m + 1; ++k1) {
                                for (int l1 = k1; l1 < m + 1; ++l1) {
                                    for (int m1 = l1; m1 < m + 1; ++m1) {
                                        for (int n1 = m1; n1 < m + 1; ++n1) {
                                            for (int i2 = n1; i2 < m + 1; ++i2) {
                                                p[0] = i;
                                                p[1] = j;
                                                p[2] = k;
                                                p[3] = l;
                                                p[4] = i1;
                                                p[5] = k1;
                                                p[6] = l1;
                                                p[7] = m1;
                                                p[8] = n1;
                                                p[9] = i2;
                                                ans = max(ans, f(p));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 3604