      module piyoMod
      implicit none
      integer(16) :: n,m,q,ans=0
      integer(16),allocatable :: abcd(:,:)
      
      contains
      recursive subroutine solve( cnt,array )
        implicit none
        integer(16),intent(in) :: cnt,array(:)
        integer(16) :: a(size(array)), i,buffer
        
        a = array
        if( cnt==1 )then
          do i = 1,m
            a(1) = i
            call solve( cnt+1,a )
          end do
        else if( cnt==n+1 )then
          
          !calc ans
          buffer = 0
          do i = 1,q
            if( a(abcd(i,2))-a(abcd(i,1))==abcd(i,3) )then
              buffer = buffer + abcd(i,4)
            end if
          end do
          ans = max( ans,buffer )
          
        else
          do i = a(cnt-1),m
            a(cnt) = i
            call solve( cnt+1,a )
          end do
        end if
      end subroutine
        
      
      end module
      
      PROGRAM ManyRequirments
      use piyoMod
      IMPLICIT NONE
      integer(16) :: i,dummy(10)
      
      read*,n,m,q
      allocate( abcd(q,4) )
      do i = 1,q
        read*,abcd(i,:)
      end do
      
      call solve(1_16,dummy(1:n))
      print*,ans
      
      
      
      
      END PROGRAM