//---------- Begin IO utilities ----------
#[macro_use]
#[allow(dead_code, unused_macros)]
mod io {
    // Thanks to https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
    macro_rules! input {
        ($($r:tt)*) => {
            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:tt : $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")
        };
    }

    macro_rules! p {
        ($v:expr) => {
            println!("{}", $v);
        };
        ($v:expr, $($t:expr),*) => {
            print!("{}", $v);
            ($(print!(" {}", $t)),*);
            println!();
        }
    }

    macro_rules! ps {
        ($v:expr) => {
            println!("{}", $v.iter().collect::<String>());
        };
        ($v:expr, $($t:expr),*) => {
            print!("{}", $v.iter().collect::<String>());
            ($(print!(" {}", $t.iter().collect::<String>())),*);
            println!();
        }
    }

    pub fn pv<T: std::string::ToString>(v: &[T], sep: &str) {
        let s = v.iter().map(|e| e.to_string()).collect::<Vec<String>>().join(sep);
        println!("{}", s);
    }

    pub fn pvs(v: &[Vec<char>], sep:&str) {
        let s = v.iter().map(|e| e.iter().collect::<String>()).collect::<Vec<String>>().join(sep);
        println!("{}", s);
    }
}
//---------- End IO utilities ----------

//---------- Start solve ----------
fn rec(now: usize, tab: &mut [usize], m: usize, v: &[(usize, usize, usize, usize)]) -> usize {
    if now == tab.len() {
        return 0;
    }
    let mut max_vals = vec![0; m+1];
    for (a, b, c, d) in v {
        if *b == now && tab[*a] + *c <= m && tab[now-1] <= tab[*a] + *c {
            tab[now] = tab[*a] + *c;
            max_vals[tab[now]] += *d;
            if max_vals[tab[now]] == *d {
                max_vals[tab[now]] += rec(now+1, tab, m, v);
            }
        }
    }
    if max_vals[tab[now-1]] == 0 {
        tab[now] = tab[now-1];
        max_vals[tab[now]] = rec(now+1, tab, m, v);
    }
    return max_vals.into_iter().max().unwrap();
}

#[allow(unused_imports)]
use io::*;
fn main() {
    input!{
        n: usize,
        m: usize,
        q: usize,
        v: [(usize1, usize1, usize, usize); q]
    }
    let mut tab = vec![0; n];
    tab[0] = 1;
    p!(rec(1, &mut tab, m, &v));
}


Measured memory_bytes: 2140