import std.stdio, std.conv, std.string, std.algorithm,
       std.math, std.array, std.container, std.typecons;

long mod = 1000000007;

long modinv(long a) {
  long b=mod, u=1, v=0;
  while(b>0) {
    long t=a/b;
    a -= t*b; swap(a,b);
    u -= t*v; swap(u,v);
  }
  u %= mod;
  if(u<0) u+=mod;
  return u;
}

long[] a, b, c, d;
long n, m, q;

long solve(long[] arr, long pos) {
  if(arr.length==pos) {
    long sum=0;
    for(long i=0; i<q; i++) {
      auto aai = arr[a[i]-1];
      auto abi = arr[b[i]-1];
      if(abi - aai == c[i]) sum += d[i];
    }
    return sum;
  }
  else {
    long maxi = 0;
    for(long i=arr[pos-1]; i<=m; i++) {
      auto a2 = arr.dup;
      a2[pos] = i;
      maxi = max(maxi, solve(a2, pos+1));
    }
    return maxi;
  }
}

void main() {
  readf("%d %d %d\n", &n, &m, &q);
  long[] arr = new long[n];
  a = new long[q];
  b = new long[q];
  c = new long[q];
  d = new long[q];
  for(long i=0; i<q; i++) {
    readf("%d %d %d %d\n", &a[i], &b[i], &c[i], &d[i]);
  }
  long maxi = 0;
  for(long i=1; i<=m; i++) {
    arr[0] = i;
    maxi = max(maxi, solve(arr, 1));
  }
  writeln(maxi);
}
