program cm
   implicit none
   integer::N,M,Q,a(50),b(50),c(50),d(50),X(10)=1,i,ans=0
   read*,N,M,Q
   do i=1,Q
      read*,a(i),b(i),c(i),d(i)
   end do
   X(1)=1
   call DFS(2)
   print"(I0)",ans
contains
recursive subroutine DFS(K)
   integer::K
   integer::i
   if(K==N+1)then
      ans=max(ans,calc())
      return
   endif
   do i=X(K-1),M
      X(K)=i
      call DFS(K+1)
   end do
end subroutine
integer function calc()
   integer::i
   calc=0
   do i=1,Q
      if(X(B(i))-X(A(i))==c(i))calc=calc+d(i)
   end do
end function
end program cm