import "dart:io";

void main(){
  List<String> inputStrList = stdin.readLineSync().split(' ');
  int n = int.parse(inputStrList[0]);
  int m = int.parse(inputStrList[1]);
  int q = int.parse(inputStrList[2]);
  List<Map<String, int>> abcd = List<Map<String, int>>();
  for(int i=0; i<q; i++){
    inputStrList = stdin.readLineSync().split(" ");
    abcd.add({
      'a': int.parse(inputStrList[0]),
      'b': int.parse(inputStrList[1]),
      'c': int.parse(inputStrList[2]),
      'd': int.parse(inputStrList[3]),
    });
  }

  List<int> capitalA = new List<int>.generate(n, (i) => 1);
  
  int max = 0;
  abcd.forEach((Map<String, int> value){
    if(capitalA[value['b'] - 1] - capitalA[value['a'] - 1] == value['c']){
      max += value['d'];
    }
  });
  
  while(_next(capitalA, m)){
    int sum = 0;
    abcd.forEach((Map<String, int> value){
      if(capitalA[value['b'] - 1] - capitalA[value['a'] - 1] == value['c']){
        sum += value['d'];
      }
    });

    if(max < sum){
      max = sum;
    }

    print(capitalA);
  }

  print(max);
}

bool _next(List<int> list, int m){
  List<int> clone = new List<int>();
  clone.addAll(list);

  int index = clone.length-1;

  int item = clone.removeLast();
  if(item != m){
    list[index] = item + 1;
    return true;
  }
  index--;

  int prevItem;
  while(!clone.isEmpty){
    prevItem = item;
    item = clone.removeLast();
    if(prevItem != item){
      list[index] = item + 1;
      return true;
    }
    index--;
  }

  return false;
}
