program sum
    implicit none
    integer i,n,m,q,a(10),d(50,4),score1,flag
    read(*,*) n,m,q
    do i = 1, q
        read(*,*) d(i,1),d(i,2),d(i,3),d(i,4)
    end do

    do i = 1, n
        a(i)=1
    end do
    
    score1=0
    flag=n
    do while ( flag>0 )
        call routine(a,d,n,m,q,score1,flag)
    end do
    write(*,*) score1
end program sum

subroutine routine(a,d,n,m,q,score1,flag)
    implicit none
    integer :: a(10), d(50,4), n, m, q, j, score, score1, flag

    score=0
    if ( flag==n ) then
        do while (a(n)<=m)
            do j = 1, q
                if ( a(d(j,2))-a(d(j,1))==d(j,3) ) then
                    score=score+d(j,4)
                end if
            end do
            if ( score>score1 ) then
                score1=score
            end if
            score=0
            a(n)=a(n)+1
        end do
        flag=flag-1
    else if ( a(flag)<m ) then
        a(flag)=a(flag)+1
        do j = flag+1, n
            a(j)=a(flag)            
        end do
        flag=n
    else
        flag=flag-1
    end if
end subroutine routine