program many_requirements
  implicit none
  integer :: n, m, q, i, x(0:10) = 0, ans = 0
  integer, dimension(50) :: a = 0, b = 0, c = 0, d = 0
  read(*,*) n, m, q
  do i = 1, q
    read(*,*) a(i), b(i), c(i), d(i)
  end do
  x(0) = 1
  call dfs(1)
  write(*,'(i0)') ans
contains
  recursive subroutine dfs(i)
    integer, intent(in) :: i
    integer :: j, tmp
    if (i == n+1) then
      tmp = 0
      do j = 1, q
        if (x(b(j))-x(a(j)) == c(j)) tmp = tmp+d(j)
      end do
      ans = max(ans,tmp)
      return
    end if
    do j = x(i-1), m
      x(i) = j
      call dfs(i+1)
    end do
  end
end program many_requirements