#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <bitset>
#include <iomanip>
#include <deque>
#include <tuple>
#include <algorithm>

using namespace std;

int main()
{
    int n, m, q;
    int a[100], b[100], c[100], d[100];
    vector<tuple<int, int, int, int>> v;
    long long int sum = 0;

    cin >> n >> m >> q;

    vector<int> ans(n+1, 0);

    for (int i = 0; i < q; i++) {
        cin >> a[i] >> b[i] >> c[i] >> d[i];
        v.emplace_back(d[i], a[i], b[i], c[i]);
    }

    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());

    for (auto x: v) {
        if (ans[get<1>(x)] == 0 && ans[get<2>(x)] == 0) {
            ans[get<1>(x)] = 1;
            ans[get<2>(x)] = get<3>(x) + 1;
            sum += get<0>(x);
        }
        else if (ans[get<1>(x)] == 0 && ans[get<2>(x)] != 0) {
            if (ans[get<2>(x)] - get<3>(x) > 0) {
                ans[get<1>(x)] = ans[get<2>(x)] - get<3>(x);
                sum += get<0>(x);
            }
        }
        else if (ans[get<1>(x)] != 0 && ans[get<2>(x)] == 0) {
            if (ans[get<1>(x)] + get<3>(x) <= n) {
                ans[get<2>(x)] = ans[get<1>(x)] + get<3>(x);
                sum += get<0>(x);
            }
        }

    }


    cout << sum << endl;
}
