program main
  integer :: n, m, q, maximum = 0, wa, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10
  integer, allocatable :: i(:, :)
  integer :: a(10)
  read (*, *) n, m, q
  allocate(i(4, 1:q))
  read (*, *) i(:, 1:q)
  
  if (n == 10) then
    do a1 = 1, m
      do a2 = a1, m
        do a3 = a2, m
          do a4 = a3, m
            do a5 = a4, m
              do a6 = a5, m
                do a7 = a6, m
                  do a8 = a7, m
                    do a9 = a8, m
                      do a10 = a9, m
                        a = (/a1, a2, a3, a4, a5, a6, a7, a8, a9, a10/)
                        wa = 0
                        do j = 1, q
                          if (a(i(2, j)) - a(i(1, j)) == i(3, j)) then
                            wa = wa + i(4, j)
                          endif
                        enddo
                        if (wa > maximum) then
                          maximum = wa
                        endif
                      enddo
                    enddo
                  enddo
                enddo
              enddo
            enddo
          enddo
        enddo
      enddo
    enddo
  endif
  
  if (n == 9) then
    do a1 = 1, m
      do a2 = a1, m
        do a3 = a2, m
          do a4 = a3, m
            do a5 = a4, m
              do a6 = a5, m
                do a7 = a6, m
                  do a8 = a7, m
                    do a9 = a8, m
                      a = (/a1, a2, a3, a4, a5, a6, a7, a8, a9, 0/)
                      wa = 0
                      do j = 1, q
                        if (a(i(2, j)) - a(i(1, j)) == i(3, j)) then
                          wa = wa + i(4, j)
                        endif
                      enddo
                      if (wa > maximum) then
                        maximum = wa
                      endif
                    enddo
                  enddo
                enddo
              enddo
            enddo
          enddo
        enddo
      enddo
    enddo
  endif
  
  if (n == 8) then
    do a1 = 1, m
      do a2 = a1, m
        do a3 = a2, m
          do a4 = a3, m
            do a5 = a4, m
              do a6 = a5, m
                do a7 = a6, m
                  do a8 = a7, m
                    a = (/a1, a2, a3, a4, a5, a6, a7, a8, 0, 0/)
                    wa = 0
                    do j = 1, q
                      if (a(i(2, j)) - a(i(1, j)) == i(3, j)) then
                        wa = wa + i(4, j)
                      endif
                    enddo
                    if (wa > maximum) then
                      maximum = wa
                    endif
                  enddo
                enddo
              enddo
            enddo
          enddo
        enddo
      enddo
    enddo
  endif
  
  if (n == 7) then
    do a1 = 1, m
      do a2 = a1, m
        do a3 = a2, m
          do a4 = a3, m
            do a5 = a4, m
              do a6 = a5, m
                do a7 = a6, m
                  a = (/a1, a2, a3, a4, a5, a6, a7, 0, 0, 0/)
                  wa = 0
                  do j = 1, q
                    if (a(i(2, j)) - a(i(1, j)) == i(3, j)) then
                      wa = wa + i(4, j)
                    endif
                  enddo
                  if (wa > maximum) then
                    maximum = wa
                  endif
                enddo
              enddo
            enddo
          enddo
        enddo
      enddo
    enddo
  endif
  
  if (n == 6) then
    do a1 = 1, m
      do a2 = a1, m
        do a3 = a2, m
          do a4 = a3, m
            do a5 = a4, m
              do a6 = a5, m
                a = (/a1, a2, a3, a4, a5, a6, 0, 0, 0, 0/)
                wa = 0
                do j = 1, q
                  if (a(i(2, j)) - a(i(1, j)) == i(3, j)) then
                    wa = wa + i(4, j)
                  endif
                enddo
                if (wa > maximum) then
                  maximum = wa
                endif
              enddo
            enddo
          enddo
        enddo
      enddo
    enddo
  endif
  
  if (n == 5) then
    do a1 = 1, m
      do a2 = a1, m
        do a3 = a2, m
          do a4 = a3, m
            do a5 = a4, m
              a = (/a1, a2, a3, a4, a5, 0, 0, 0, 0, 0/)
              wa = 0
              do j = 1, q
                if (a(i(2, j)) - a(i(1, j)) == i(3, j)) then
                  wa = wa + i(4, j)
                endif
              enddo
              if (wa > maximum) then
                maximum = wa
              endif
            enddo
          enddo
        enddo
      enddo
    enddo
  endif
  
  if (n == 4) then
    do a1 = 1, m
      do a2 = a1, m
        do a3 = a2, m
          do a4 = a3, m
            a = (/a1, a2, a3, a4, 0, 0, 0, 0, 0, 0/)
            wa = 0
            do j = 1, q
              if (a(i(2, j)) - a(i(1, j)) == i(3, j)) then
                wa = wa + i(4, j)
              endif
            enddo
            if (wa > maximum) then
              maximum = wa
            endif
          enddo
        enddo
      enddo
    enddo
  endif
  
  if (n == 3) then
    do a1 = 1, m
      do a2 = a1, m
        do a3 = a2, m
          a = (/a1, a2, a3, 0, 0, 0, 0, 0, 0, 0/)
          wa = 0
          do j = 1, q
            if (a(i(2, j)) - a(i(1, j)) == i(3, j)) then
              wa = wa + i(4, j)
            endif
          enddo
          if (wa > maximum) then
            maximum = wa
          endif
        enddo
      enddo
    enddo
  endif
  
  if (n == 2) then
    do a1 = 1, m
      do a2 = a1, m
        a = (/a1, a2, 0, 0, 0, 0, 0, 0, 0, 0/)
        wa = 0
        do j = 1, q
          if (a(i(2, j)) - a(i(1, j)) == i(3, j)) then
            wa = wa + i(4, j)
          endif
        enddo
        if (wa > maximum) then
          maximum = wa
        endif
      enddo
    enddo
  endif
  
  write (*, '(i0)') maximum
end program main


Measured memory_bytes: 2796