program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,m,q,i,ansv
    integer(int32), allocatable:: a(:),b(:),c(:),d(:), ij(:,:,:),ans(:)

    read*, n,m,q
    allocate(a(q),b(q),c(q),d(q),ans(n),ij(n,n,2))
    ij(:,:,:) = 0
    do i=1,q
        read*, a(i),b(i),c(i),d(i)
        ij(a(i),b(i),1) = c(i)
        ij(a(i),b(i),2) = d(i)
    end do

    ans(:) = 1
    ansv=0

    do
        ansv = max(calc_sum(),ansv)
        if (next()) exit
    end do

    print'(i0)', ansv
contains
    function calc_sum() result(ret)
        integer(int32):: ret
        integer(int32):: i

        ret=0
        do i=1,q
            if (ans(b(i))-ans(a(i)) == c(i)) ret=ret+d(i)
        end do
    end function

    
    function next() result(ret)
        integer(int32):: i 
        logical:: ret

        ret = .false.
        i=n
        do while(ans(i) >= m)
            i=i-1
            if (i==0) then
                ret = .true.
                return
            end if
        end do
        ans(i:n) = ans(i)+1
    end function
end program name