program main
  
  implicit none
  integer :: nn, mm, qq, ii, tmp
  integer(8) :: tmpans, ans
  integer,allocatable :: a(:)
  integer,allocatable :: b(:)
  integer,allocatable :: c(:)
  integer(8),allocatable :: d(:)
  integer,allocatable :: aa(:)
  integer :: i,j,k,l,m,n,o,p,q,r,s
  
  read(*,*) nn, mm, qq
  allocate(a(1:qq))
  allocate(b(1:qq))
  allocate(c(1:qq))
  allocate(d(1:qq))
  allocate(aa(1:n))
  
  do i = 1, qq
    read(*,*) a(i), b(i), c(i), d(i)
  end do

  aa = 0 
  ans = 0
  do i = 1, mm
    aa(1) = i
    do j = 1, mm
      aa(2) = j
      if ( nn == 2 ) then
        tmpans = 0
        do ii = 1, qq
          tmp = aa(b(ii))-aa(a(ii))
          if( tmp == c(ii) ) tmpans = tmpans + d(ii)
        end do
        if( tmpans >= ans ) ans = tmpans
      else 
        do k = 1, mm
          aa(3) = k
          if ( nn == 3 ) then
            tmpans = 0
            do ii = 1, qq
              tmp = aa(b(ii))-aa(a(ii))
              if( tmp == c(ii) ) tmpans = tmpans + d(ii)
            end do
            if( tmpans >= ans ) ans = tmpans
          else 
            do l = 1, mm
              aa(4) = l
              if ( nn == 4 ) then
                tmpans = 0
                do ii = 1, qq
                  tmp = aa(b(ii))-aa(a(ii))
                  if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                end do
                if( tmpans >= ans ) ans = tmpans
              else 
                do m = 1, mm
                  aa(5) = m
                  if ( nn == 5 ) then
                    tmpans = 0
                    do ii = 1, qq
                      tmp = aa(b(ii))-aa(a(ii))
                      if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                    end do
                    if( tmpans >= ans ) ans = tmpans
                  else 
                    do n = 1, mm
                      aa(6) = n
                      if ( nn == 6 ) then
                        tmpans = 0
                        do ii = 1, qq
                          tmp = aa(b(ii))-aa(a(ii))
                          if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                        end do
                        if( tmpans >= ans ) ans = tmpans
                      else 
                        do o = 1, mm
                          aa(7) = o
                          if ( nn == 7 ) then
                            tmpans = 0
                            do ii = 1, qq
                              tmp = aa(b(ii))-aa(a(ii))
                              if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                            end do
                            if( tmpans >= ans ) ans = tmpans
                          else 
                            do p = 1, mm
                              aa(8) = p
                              if ( nn == 8 ) then
                                tmpans = 0
                                do ii = 1, qq
                                  tmp = aa(b(ii))-aa(a(ii))
                                  if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                                end do
                                if( tmpans >= ans ) ans = tmpans
                              else 
                                do q = 1, mm
                                  aa(9) = q
                                  if ( nn == 9 ) then
                                    tmpans = 0
                                    do ii = 1, qq
                                      tmp = aa(b(ii))-aa(a(ii))
                                      if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                                    end do
                                    if( tmpans >= ans ) ans = tmpans
                                  else 
                                    do r = 1, mm
                                      aa(10) = r
                                      if ( nn == 10 ) then
                                        tmpans = 0
                                        do ii = 1, qq
                                          tmp = aa(b(ii))-aa(a(ii))
                                          if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                                        end do
                                        if( tmpans >= ans ) ans = tmpans
                                      else 
                                      end if
                                    end do
                                  end if
                                end do
                              end if
                            end do
                          end if
                        end do
                      end if
                    end do
                  end if
                end do
              end if
            end do
          end if
        end do
      end if
    end do
  end do
  
  print*, ans
  
  
end program main
