import std;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    long N, M, Q;
    scan(N, M, Q);
    auto a = new long[](Q);
    auto b = new long[](Q);
    auto c = new long[](Q);
    auto d = new long[](Q);
    foreach (i; 0 .. Q)
        scan(a[i], b[i], c[i], d[i]);
    zip(d, a, b, c).sort!"b<a"();
    long ansm;
    foreach (state; 0 .. 2 ^^ Q)
    {
        long ans;
        auto A = new long[](N + 1);
        A[] = long.min;
        foreach (i; 0 .. Q)
        {
            if ((state >> i) & 1)
                continue;
            // writeln(a[i], b[i], c[i], d[i]);
            if (A[a[i]] == long.min && A[b[i]] == long.min)
            {
                A[a[i]] = 0;
                A[b[i]] = c[i];
                ans += d[i];
                continue;
            }
            if (A[a[i]] == long.min && A[b[i]] != long.min)
            {
                A[a[i]] = A[b[i]] - c[i];
                ans += d[i];
                continue;
            }
            if (A[b[i]] == long.min && A[a[i]] != long.min)
            {
                A[b[i]] = c[i] + A[a[i]];
                ans += d[i];
                continue;
            }
            if (A[a[i]] != long.min && A[b[i]] != long.min && A[b[i]] - A[a[i]] == c[i])
            {
                ans += d[i];
                continue;
            }
            // writeln(A);
            // writeln(b[i], " ", a[i]);
            // writeln(A[b[i]], " ", A[a[i]]);
            // assert(0);
        }
        ansm = ansm.max(ans);
    }
    // writeln(A);
    writeln(ansm);
}
