typealias G = (Int, Int, Int, Int)

let inp = readLine()!.split(separator: " ").map { Int($0)! }
let (N, M, Q) = (inp[0], inp[1], inp[2])
let gs: [G] = (1...Q).map { _ -> G in
        let a = readLine()!.split(separator: " ").map { Int($0)! }
        return (a[0], a[1], a[2], a[3])
}.sorted { $0.3 < $1.3 }

var A = Array(repeating: 0, count: N)
for g in gs {
    let a = A[g.0-1]
    A[g.1-1] = a + g.2
}

let r = gs.reduce(0) { (n: Int, g: G) in
    ((A[g.1-1] - A[g.0-1]) == g.2) ? n + g.3 : n
}
print(r)