program sum
    implicit none
    integer i,j,n,m,q,a(10),d(50,4),c,score,score1,flag
    read(*,*) n,m,q
    c=1
    score=0
    score1=0
    flag=0
    do i = 1, q
        read(*,*) d(i,1),d(i,2),d(i,3),d(i,4)
    end do
    do i = 1, n
        c=c*(11-i)
    end do
    do i = 2, n
        c=c/i
    end do
    do i = 1, n
        a(i)=i
    end do
    do while (a(n)<=10)
        ! do i = 1, n
        !     write(*, fmt='(I4)', advance='no') a(i)
        ! end do
        ! Write(*,*)
        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-flag)=a(n-flag)+1
        if ( flag==n-1 ) then
            flag=0
        else
            flag=flag+1
        end if
    end do
    write(*,*) score1
end program sum