program ABC165_C
  integer::N,M,Q,x(10),ans=0
  integer,allocatable::a(:),b(:),c(:),d(i)
  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
  x(1)=1
  call DFS(2)
  write(*,*)ans
contains
  recursive subroutine DFS(K)
    integer::K
    if(K==N+1) then !AをN個決定した(11...1~MM...M)
       ans=max(ans,calc())
       return
    end if
    do i=x(K-1),M !Aを小さい順になるよう決めていく．
       x(K)=i
       call DFS(K+1)
    end do
  end subroutine DFS
  integer function calc()
    calc=0
    do i=1,Q
       if(x(B(i))-x(A(i))==c(i)) calc=calc+d(i)
    end do
  end function calc
end program ABC165_C