#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>
#include<stdbool.h>
#include<tgmath.h>
#include<time.h>
#define ll long long
#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define miin(a, b, c) min((a), min((b), (c)))
#define maax(a, b, c) max((a), max((b), (c)))
#define mod 1000000007

int n,m,q,a[55][4],b[11],c=0,d;

main(){
    scanf("%d%d%d",&n,&m,&q);
    for(int i=0;i<q;i++){
        for(int j=0;j<4;j++) scanf("%d",&a[i][j]);
    }

    for(b[0]=1;b[0]<=m;b[0]++){
        for(b[1]=b[0];b[1]<=m;b[1]++){
            for(b[2]=b[1];b[2]<=m;b[2]++){
                for(b[3]=b[2];b[3]<=m;b[3]++){
                    for(b[4]=b[3];b[4]<=m;b[4]++){
                        for(b[5]=b[4];b[5]<=m;b[5]++){
                            for(b[6]=b[5];b[6]<=m;b[6]++){
                                for(b[7]=b[6];b[7]<=m;b[7]++){
                                    for(b[8]=b[7];b[8]<=m;b[8]++){
                                        for(b[9]=b[8];b[9]<=m;b[9]++){
                                            d=0;
                                            for(int i=0;i<q;i++){
                                                if(b[a[i][1]-1] - b[a[i][0]-1] == a[i][2]){
                                                    d += a[i][3];
                                                }
                                            }
                                            c = max(c, d);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    printf("%d\n", c);
    return 0;
}