program cm
   implicit none
   integer::N,M,Q
   integer,allocatable,dimension(:)::a,b,c,d
   integer,allocatable,dimension(:)::X

   integer::i
   integer::ans=0
   read*,N,M,Q
   allocate(a(Q),b(Q),c(Q),d(Q))
   do i=1,Q
      read*,a(i),b(i),c(i),d(i)
   end do

   allocate(X(0:N))
   X(0)=1
   call DFS(1)
   print"(I0)",ans
contains
recursive subroutine DFS(K)
   integer::K
   integer::tmp
   integer::i
   if(K==N+1)then
      tmp=0
      do i=1,Q
         if(X( B(i) )-X( A(i) )==c(i))tmp=tmp+d(i)
      end do
      ans=max(ans,tmp)
      return
   endif
   do i=X(K-1),M
      X(K)=i
      call DFS(K+1)
   end do
end subroutine
end program cm