macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
//        let mut s = {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

// x ** n % mods を求める
pub fn mod_pow(x: i64, n: i64, mods: i64) -> i64 {
    if n == 0 {
        return 1;
    }
    let mut res = mod_pow(x * x % mods, n / 2, mods);
    if n % 2 == 1 {
        res = res * x % mods;
    }
    return res;
}

// (1/n) % mods を求める
pub fn mod_inv(n: i64, mods: i64) -> i64 {
    return mod_pow(n, mods-2, mods);
}

// nCk % mods を求める
pub fn mod_comb(n: i64, k: i64, mods: i64) -> i64 {
    let sum1 = (n-k+1..n+1).fold(1, |ac, x| ac * x % mods);
    let sum2 = (1..k+1).fold(1, |ac, x| ac * x % mods);
    return sum1 * mod_inv(sum2, mods) % mods;
}

use std::cmp;

fn factorial(a: usize) -> usize {
    let mut m = a;
    let mut ans = 1;
    while m > 1 {
        ans *= m;
        m -= 1;
    };
    ans
}
 
fn multi_choose(size: usize, max_value: usize) -> Vec<Vec<usize>> {
    let len = factorial(size + max_value - 1) / factorial(size) / factorial(max_value - 1);
    let mut ans = Vec::new();
    ans.push(vec![max_value; size]);
    let mut hold = vec![max_value; size];
    for _ in 0..len-1 {
        let mut v = 0;
        loop {
            hold[v] -= 1;
            if hold[v] == 0 {
                hold[v] = max_value;
                v += 1;
                continue;
            }
            break;
        }
        for i in 1..size {
            hold[size - i - 1] = cmp::min(hold[size - i - 1], hold[size - i]);
        }
        ans.push(hold.to_vec());
    }
    ans
}

//use combinations::Combinations;

fn main() {
    input!{
        n: usize,
        m: usize,
        q: usize,
        sabcd: [[usize; 4]; q],
    }
//    let it: Vec<_> = Combinations::new((1..n+m).collect(), n).collect();
    let it = multi_choose(n, m);
    let mut max = 0;
    let mut tmp;
//    let mut a;
//    for iter in it.iter() {
//        println!("{:?}", it);
    for iter in it {
        // a = vec![0; n];
        // a[0] = iter[0];
        // for i in 1..n {
        //     a[i] = a[i-1] + iter[i] - iter[i-1] - 1; 
        // }
        tmp = 0;
        for j in 0..q {
//            if a[sabcd[j][1]-1] - a[sabcd[j][0]-1] == sabcd[j][2] {
            if iter[sabcd[j][1]-1] - iter[sabcd[j][0]-1] == sabcd[j][2] {
                tmp = tmp + sabcd[j][3];
            }
        }
        if tmp > max {
            max = tmp;
        }
    }
    println!("{}", max);
}


Measured memory_bytes: 12712