#include<cstdio>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<tuple>
#include<algorithm>
#include<numeric>
#include<limits>
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
#define BEGIN ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define END return EXIT_SUCCESS
#define up(I,A,B) for(auto I=(A);I<=(B);++I)
#define dw(I,A,B) for(auto I=(A);I>=(B);--I)
#define rep(I,N) up(I,0,N-1)
#define all(C) (C).begin(),(C).end()
#define rall(C) (C).rbegin(),(C).rend()
#define ft first
#define sd second
#define mp make_pair
#define mt make_tuple
#define pf push_front
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define rs resize
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>inline void                                      in(T &p){cin>>p;}
template<class T1,class T2>inline void                            in(T1 &p1,T2 &p2){cin>>p1>>p2;}
template<class T1,class T2,class T3>inline void                   in(T1 &p1,T2 &p2,T3 &p3){cin>>p1>>p2>>p3;}
template<class T1,class T2,class T3,class T4>inline void          in(T1 &p1,T2 &p2,T3 &p3,T4 &p4){cin>>p1>>p2>>p3>>p4;}
template<class T1,class T2,class T3,class T4,class T5>inline void in(T1 &p1,T2 &p2,T3 &p3,T4 &p4,T5 &p5){cin>>p1>>p2>>p3>>p4>>p5;}
template<class T>inline void                                      ins(T &p){for_each(all(p),in<decltype(p[0])>);}
template<class T>inline void                             out(T p){cout<<p<<"\n";}
template<class T1,class T2>inline void                   out(T1 p1,T2 p2){cout<<p1<<" "<<p2<<"\n";}
template<class T1,class T2,class T3>inline void          out(T1 p1,T2 p2,T3 p3){cout<<p1<<" "<<p2<<" "<<p3<<"\n";}
template<class T1,class T2,class T3,class T4>inline void out(T1 p1,T2 p2,T3 p3,T4 p4){cout<<p1<<" "<<p2<<" "<<p3<<" "<<p4<<"\n";}
inline void                                              out(ld p){printf("%.12Lf\n",p);}
template<class T>inline void outml(T p){for_each(all(p),out<decltype(p[0])>);}//ml=multi line
template<class T>inline void outol(T V){auto n=V.size();rep(i,n)cout<<V[i]<<" \n"[i==n-1];}//ol=one line
inline void solve(void){
  int N,M,Q;
  in(N,M,Q);
  vector<long>A,B,C,D;
  rep(i,Q){
    int a,b,c,d;
    in(a,b,c,d);
    A.pb(a);
    B.pb(b);
    C.pb(c);
    D.pb(d);
  }
  long ans=-1;
  if(N==2){
    up(i,1,M){
      up(j,i,M){
        long tmp=0;
        rep(k,Q){
          if(j-i==C[k])tmp+=D[k];
        }
        chmax(ans,tmp);
      }
    }
  }
  if(N==3){
    up(i,1,M){
      up(j,i,M){
        up(k,j,M){
          long tmp=0;
          rep(l,Q){
            long x,y;
            if(A[l]==1)x=i;
            if(A[l]==2)x=j;
            if(A[l]==3)x=k;
            if(B[l]==1)y=i;
            if(B[l]==2)y=j;
            if(B[l]==3)y=k;
            if(y-x==C[l])tmp+=D[l];
          }
          chmax(ans,tmp);
        }
      }
    }
  }
  if(N==4){
    up(i1,1,M){
      up(i2,i1,M){
        up(i3,i2,M){
          up(i4,i3,M){
            long tmp=0;
            rep(j,Q){
              long x,y;
              if(A[j]==1)x=i1;
              if(A[j]==2)x=i2;
              if(A[j]==3)x=i3;
              if(A[j]==4)x=i4;
              if(B[j]==1)y=i1;
              if(B[j]==2)y=i2;
              if(B[j]==3)y=i3;
              if(B[j]==4)y=i4;
              if(y-x==C[j])tmp+=D[j];
            }
            chmax(ans,tmp);
          }
        }
      }
    }
  }
  if(N==5){
    up(i1,1,M){
      up(i2,i1,M){
        up(i3,i2,M){
          up(i4,i3,M){
            up(i5,i4,M){
              long tmp=0;
              rep(j,Q){
                long x,y;
                if(A[j]==1)x=i1;
                if(A[j]==2)x=i2;
                if(A[j]==3)x=i3;
                if(A[j]==4)x=i4;
                if(A[j]==5)x=i5;
                if(B[j]==1)y=i1;
                if(B[j]==2)y=i2;
                if(B[j]==3)y=i3;
                if(B[j]==4)y=i4;
                if(B[j]==5)y=i5;
                if(y-x==C[j])tmp+=D[j];
              }
              chmax(ans,tmp);
            }
          }
        }
      }
    }
  }
  if(N==6){
    up(i1,1,M){
      up(i2,i1,M){
        up(i3,i2,M){
          up(i4,i3,M){
            up(i5,i4,M){
              up(i6,i5,M){
                long tmp=0;
                rep(j,Q){
                  long x,y;
                  if(A[j]==1)x=i1;
                  if(A[j]==2)x=i2;
                  if(A[j]==3)x=i3;
                  if(A[j]==4)x=i4;
                  if(A[j]==5)x=i5;
                  if(A[j]==6)x=i6;
                  if(B[j]==1)y=i1;
                  if(B[j]==2)y=i2;
                  if(B[j]==3)y=i3;
                  if(B[j]==4)y=i4;
                  if(B[j]==5)y=i5;
                  if(B[j]==6)y=i6;
                  if(y-x==C[j])tmp+=D[j];
                }
                chmax(ans,tmp);
              }
            }
          }
        }
      }
    }
  }
  if(N==7){
    up(i1,1,M){
      up(i2,i1,M){
        up(i3,i2,M){
          up(i4,i3,M){
            up(i5,i4,M){
              up(i6,i5,M){
                up(i7,i6,M){
                  long tmp=0;
                  rep(j,Q){
                    long x,y;
                    if(A[j]==1)x=i1;
                    if(A[j]==2)x=i2;
                    if(A[j]==3)x=i3;
                    if(A[j]==4)x=i4;
                    if(A[j]==5)x=i5;
                    if(A[j]==6)x=i6;
                    if(A[j]==7)x=i7;
                    if(B[j]==1)y=i1;
                    if(B[j]==2)y=i2;
                    if(B[j]==3)y=i3;
                    if(B[j]==4)y=i4;
                    if(B[j]==5)y=i5;
                    if(B[j]==6)y=i6;
                    if(B[j]==7)y=i7;
                    if(y-x==C[j])tmp+=D[j];
                  }
                  chmax(ans,tmp);
                }
              }
            }
          }
        }
      }
    }
  }
  if(N==8){
    up(i1,1,M){
      up(i2,i1,M){
        up(i3,i2,M){
          up(i4,i3,M){
            up(i5,i4,M){
              up(i6,i5,M){
                up(i7,i6,M){
                  up(i8,i7,M){
                    long tmp=0;
                    rep(j,Q){
                      long x,y;
                      if(A[j]==1)x=i1;
                      if(A[j]==2)x=i2;
                      if(A[j]==3)x=i3;
                      if(A[j]==4)x=i4;
                      if(A[j]==5)x=i5;
                      if(A[j]==6)x=i6;
                      if(A[j]==7)x=i7;
                      if(A[j]==8)x=i8;
                      if(B[j]==1)y=i1;
                      if(B[j]==2)y=i2;
                      if(B[j]==3)y=i3;
                      if(B[j]==4)y=i4;
                      if(B[j]==5)y=i5;
                      if(B[j]==6)y=i6;
                      if(B[j]==7)y=i7;
                      if(B[j]==8)y=i8;
                      if(y-x==C[j])tmp+=D[j];
                    }
                    chmax(ans,tmp);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  if(N==9){
    up(i1,1,M){
      up(i2,i1,M){
        up(i3,i2,M){
          up(i4,i3,M){
            up(i5,i4,M){
              up(i6,i5,M){
                up(i7,i6,M){
                  up(i8,i7,M){
                    up(i9,i8,M){
                      long tmp=0;
                      rep(j,Q){
                        long x,y;
                        if(A[j]==1)x=i1;
                        if(A[j]==2)x=i2;
                        if(A[j]==3)x=i3;
                        if(A[j]==4)x=i4;
                        if(A[j]==5)x=i5;
                        if(A[j]==6)x=i6;
                        if(A[j]==7)x=i7;
                        if(A[j]==8)x=i8;
                        if(A[j]==9)x=i9;
                        if(B[j]==1)y=i1;
                        if(B[j]==2)y=i2;
                        if(B[j]==3)y=i3;
                        if(B[j]==4)y=i4;
                        if(B[j]==5)y=i5;
                        if(B[j]==6)y=i6;
                        if(B[j]==7)y=i7;
                        if(B[j]==8)y=i8;
                        if(B[j]==9)y=i9;
                        if(y-x==C[j])tmp+=D[j];
                      }
                      chmax(ans,tmp);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  if(N==10){
    up(i1,1,M){
      up(i2,i1,M){
        up(i3,i2,M){
          up(i4,i3,M){
            up(i5,i4,M){
              up(i6,i5,M){
                up(i7,i6,M){
                  up(i8,i7,M){
                    up(i9,i8,M){
                      up(i10,i9,M){
                        long tmp=0;
                        rep(j,Q){
                          long x,y;
                          if(A[j]==1)x=i1;
                          if(A[j]==2)x=i2;
                          if(A[j]==3)x=i3;
                          if(A[j]==4)x=i4;
                          if(A[j]==5)x=i5;
                          if(A[j]==6)x=i6;
                          if(A[j]==7)x=i7;
                          if(A[j]==8)x=i8;
                          if(A[j]==9)x=i9;
                          if(A[j]==10)x=i10;
                          if(B[j]==1)y=i1;
                          if(B[j]==2)y=i2;
                          if(B[j]==3)y=i3;
                          if(B[j]==4)y=i4;
                          if(B[j]==5)y=i5;
                          if(B[j]==6)y=i6;
                          if(B[j]==7)y=i7;
                          if(B[j]==8)y=i8;
                          if(B[j]==9)y=i9;
                          if(B[j]==10)y=i10;
                          if(y-x==C[j])tmp+=D[j];
                        }
                        chmax(ans,tmp);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  out(ans);
}
int main(void){
  BEGIN;
  solve();
  END;
}


Measured memory_bytes: 3596