struct Read<R: std::io::BufRead> {
    stdin: R,
    v: Vec<String>,
    i: usize,
}
impl<R:std::io::BufRead> Read<R> {
    pub fn new(stdin: R) -> Self {
        Read{
            stdin: stdin,
            v: Vec::new(),
            i: 0,
        }
    }
    pub fn read_line(&mut self) {
        self.i = 0; self.v.clear();
        let mut buf = String::new();
        self.stdin.read_line(&mut buf).unwrap();
        let buf = buf.trim().to_owned();
        let mut ws = buf.split_whitespace();
        loop {
            let x = ws.next();
            if x.is_none() {break;}
            let x = x.unwrap().to_owned();
            self.v.push(x);
        }
    }
    #[allow(dead_code)]
    pub fn read_t<T: std::str::FromStr>(&mut self) -> T {
        if self.i == self.v.len() {self.read_line();}
        let elem = &self.v[self.i];
        self.i += 1;
        elem.parse().unwrap_or_else(|_| panic!())
    }
    
    #[allow(dead_code)]
    pub fn read_i32(&mut self) -> i32 {
        if self.i == self.v.len() {self.read_line();}
        let elem = self.v[self.i].parse().unwrap();
        self.i += 1;
        elem
    }
    #[allow(dead_code)]
    pub fn read_usize(&mut self) -> usize {
        if self.i == self.v.len() {self.read_line();}
        let elem = self.v[self.i].parse().unwrap();
        self.i += 1;
        elem
    }
    #[allow(dead_code)]
    pub fn read_u64(&mut self) -> u64 {
        if self.i == self.v.len() {self.read_line();}
        let elem = self.v[self.i].parse().unwrap();
        self.i += 1;
        elem
    }
    #[allow(dead_code)]
    pub fn read_string(&mut self) -> String {
        if self.i == self.v.len() {self.read_line();}
        let elem = &self.v[self.i];
        self.i += 1;
        elem.to_owned()
    }
}

fn main() {
    use std::io;
    let stdin = io::stdin();
    let stdin = io::BufReader::new(stdin.lock());
    let read = Read::new(stdin);
    solve(read);
}


fn solve<R: std::io::BufRead>(mut r: Read<R>) {
    let n = r.read_i32();
    let m = r.read_i32();
    let q = r.read_i32();
    let mut a:Vec<i32> = Vec::new();
    let mut b:Vec<i32> = Vec::new();
    let mut c:Vec<i32> = Vec::new();
    let mut d:Vec<i32> = Vec::new();
    for _i in 0..q {
        a.push(r.read_i32());
        b.push(r.read_i32());
        c.push(r.read_i32());
        d.push(r.read_i32());
    }
    let mut x:Vec<i32> = Vec::new();
    for _i in 0..n {
        x.push(1);
    }
    let mut ans:i32 = 0;
    loop {
        let mut sum:i32 = 0;
        for i in 0..q as usize {
            if x[b[i] as usize - 1] - x[a[i] as usize - 1] == c[i] {
                sum += d[i];
            }
        }
        ans = if sum > ans {
            sum
        }else{
            ans
        };
        if make_indexes(&mut x, &m) {
            break;
        }
    }

    println!("{}", ans);
}

fn make_indexes(a: &mut Vec<i32>, m: &i32) -> bool{
    for i in (0..a.len()).rev() {
        if a[i] < *m {
            a[i] += 1;
            for j in (i+1)..a.len() {
                a[j] = a[i];
            }
            return false;
        }
    }

    true
}