import std;

alias S = Tuple!(int, "a", int, "b", int, "c", int, "d");

int calc(int n, int m, S[] s) {
    int ans = 0;

    void rec(int last, int p, int[] buf) {
        if (p == buf.length) {
            int t = 0;
            foreach (e; s) {
                int x = buf[e.b - 1] - buf[e.a - 1];
                if (x == e.c) t += e.d;
            }
            ans = max(ans, t);
            return;
        }


        for (int i = last; i <= m; i++) {
            buf[p] = i;
            rec(i, p + 1, buf);
        }
    }

    auto buf = new int[n];
    rec(1, 0, buf);
    return ans;
}

void main() {
    int n, m, q; scan(n, m, q);
    S[] s;
    foreach (_; 0..q) {
        int a, b, c, d; scan(a, b, c, d);
        s ~= S(a, b, c, d);
    }
    writeln(calc(n, m, s));
}

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
