#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[macro_export]
macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut parser = Parser :: from_str ( $ s ) ; input_inner ! { parser , $ ( $ r ) * } } ; ( parser = $ parser : ident , $ ( $ r : tt ) * ) => { input_inner ! { $ parser , $ ( $ r ) * } } ; ( new_stdin_parser = $ parser : ident , $ ( $ r : tt ) * ) => { let stdin = std :: io :: stdin ( ) ; let reader = std :: io :: BufReader :: new ( stdin . lock ( ) ) ; let mut $ parser = Parser :: new ( reader ) ; input_inner ! { $ parser , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { input ! { new_stdin_parser = parser , $ ( $ r ) * } } ; }
#[macro_export]
macro_rules ! input_inner { ( $ parser : ident ) => { } ; ( $ parser : ident , ) => { } ; ( $ parser : ident , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ parser , $ t ) ; input_inner ! { $ parser $ ( $ r ) * } } ; }
#[macro_export]
macro_rules ! read_value { ( $ parser : ident , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ parser , $ t ) ) ,* ) } ; ( $ parser : ident , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ parser , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ parser : ident , chars ) => { read_value ! ( $ parser , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ parser : ident , usize1 ) => { read_value ! ( $ parser , usize ) - 1 } ; ( $ parser : ident , $ t : ty ) => { $ parser . next ::<$ t > ( ) . expect ( "Parse error" ) } ; }
use std::io;
use std::io::BufRead;
use std::str;
pub struct Parser<R> {
    reader: R,
    buf: Vec<u8>,
    pos: usize,
}
impl Parser<io::Empty> {
    pub fn from_str(s: &str) -> Parser<io::Empty> {
        Parser {
            reader: io::empty(),
            buf: s.as_bytes().to_vec(),
            pos: 0,
        }
    }
}
impl<R: BufRead> Parser<R> {
    pub fn new(reader: R) -> Parser<R> {
        Parser {
            reader: reader,
            buf: vec![],
            pos: 0,
        }
    }
    pub fn update_buf(&mut self) {
        self.buf.clear();
        self.pos = 0;
        loop {
            let (len, complete) = {
                let buf2 = self.reader.fill_buf().unwrap();
                self.buf.extend_from_slice(buf2);
                let len = buf2.len();
                if len == 0 {
                    break;
                }
                (len, buf2[len - 1] <= 0x20)
            };
            self.reader.consume(len);
            if complete {
                break;
            }
        }
    }
    pub fn next<T: str::FromStr>(&mut self) -> Result<T, T::Err> {
        loop {
            let mut begin = self.pos;
            while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                begin += 1;
            }
            let mut end = begin;
            while end < self.buf.len() && (self.buf[end] > 0x20) {
                end += 1;
            }
            if begin != self.buf.len() {
                self.pos = end;
                return str::from_utf8(&self.buf[begin..end]).unwrap().parse::<T>();
            } else {
                self.update_buf();
            }
        }
    }
}
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
const BIG_STACK_SIZE: bool = true;
#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}
fn solve() {
    solve0();
    // solve1();
}
fn solve0() {
    input! {
        V:usize,E:usize,F:i64,
        ES:[(usize,usize,i64,i64);E],
    }
    let mut network = Network::new(V);
    for (u,v,cap,cost) in ES { 
        network.add_edge(u, v, cap, cost);
    }
    let res = network.min_cost_flow(0, V-1, F);
    if res.is_none() {
        println!("-1");
    } else {
        println!("{}",res.unwrap());
    }

    #[derive(Clone)]
    struct Edge {
        to: usize,
        cap: i64,
        cost: i64,
        rev: usize,
    }

    struct Network {
        g: Vec<Vec<Edge>>,
    }

    impl Network {
        fn new(n: usize) -> Network {
            Network {
                g: vec![vec![]; n],
            }
        }
    
        fn add_edge(&mut self, from: usize, to: usize, cap: i64, cost: i64) {
            let from_rev = self.g[to].len();
            let to_rev = self.g[from].len();
            self.g[from].push(Edge {
                to: to,
                cap: cap,
                cost: cost,
                rev: from_rev,
            });
            self.g[to].push(Edge {
                to: from,
                cap: 0,
                cost: -1 * cost,
                rev: to_rev,
            });
        } 

        fn n(&self) -> usize {
            self.g.len()
        }

        fn min_cost_flow(&mut self, s: usize, t: usize, f: i64) -> Option<i64> {
            let mut res = 0;
            let mut total_flow = f;
            let mut prevv = vec![0; self.n()];
            let mut preve = vec![0; self.n()];

            let mut h = vec![0; self.n()];

            while total_flow > 0 {
                let inf = 2_000_000_001;
                let mut queue = std::collections::BinaryHeap::new(); // max-heap
                let mut dist = vec![inf; self.n()]; // for all >= 0
                dist[s] = 0;
                queue.push((0, s));

                while let Some((neg_cost, v)) = queue.pop() {
                    let cost = -neg_cost; // 逆転させる
                    if dist[v] < cost { continue; }
                    for i in 0..self.g[v].len() {
                        let e = &self.g[v][i];
                        let new_dist = dist[v] + e.cost + h[v] - h[e.to];
                        if e.cap > 0 && dist[e.to] > new_dist {
                            dist[e.to] = new_dist;
                            prevv[e.to] = v;
                            preve[e.to] = i;
                            queue.push((-dist[e.to], e.to)); // 負値にして格納する
                        }
                    }
                }
                if dist[t] == inf {
                    return None;
                }
                for v in 0..self.n() {
                    h[v] += dist[v];
                }
                
                let mut actual_flow = total_flow;
                let mut v = t;
                loop {
                    if v == s { break; }
                    actual_flow = std::cmp::min(actual_flow, self.g[prevv[v]][preve[v]].cap);
                    v = prevv[v];
                }

                total_flow -= actual_flow;
                res += actual_flow * h[t];
                let mut v = t;
                loop {
                    if v == s { break; }
                    let e = self.g[prevv[v]][preve[v]].clone();
                    self.g[prevv[v]][preve[v]].cap -= actual_flow;
                    self.g[v][e.rev].cap += actual_flow;
                    v = prevv[v];
                }
            }

            Some(res)
        }
    }
}
// AC
fn solve1() {
    input! {
        V:usize,E:usize,F:i64,
        ES:[(usize,usize,i64,i64);E],
    }
    let mut network = Network::new(V);
    for (u,v,cap,cost) in ES { 
        network.add_edge(u, v, cap, cost);
    }
    let res = network.min_cost_flow(0, V-1, F);
    if res.is_none() {
        println!("-1");
    } else {
        println!("{}",res.unwrap());
    }

    #[derive(Clone)]
    struct Edge {
        to: usize,
        cap: i64,
        cost: i64,
        rev: usize,
    }

    struct Network {
        g: Vec<Vec<Edge>>,
    }

    impl Network {
        fn new(n: usize) -> Network {
            Network {
                g: vec![vec![]; n],
            }
        }
        /// allows negative costs
        fn add_edge(&mut self, from: usize, to: usize, cap: i64, cost: i64) {
            let from_rev = self.g[to].len();
            let to_rev = self.g[from].len();
            self.g[from].push(Edge {
                to: to,
                cap: cap,
                cost: cost,
                rev: from_rev,
            });
            self.g[to].push(Edge {
                to: from,
                cap: 0,
                cost: -cost,
                rev: to_rev,
            });
        }

        fn n(&self) -> usize {
            self.g.len()
        }

        fn min_cost_flow(&mut self, s: usize, t: usize, f: i64) -> Option<i64> {
            let mut res = 0;
            let mut prevv = vec![0; self.n()];
            let mut preve = vec![0; self.n()];
            let mut f = f;
            let inf = 2_000_000_001;

            while f > 0 {
                let mut dist = vec![inf; self.n()];
                dist[s] = 0;
                let mut update = true;
                while update {
                    update = false;
                    for v in 0..self.n() {
                        if dist[v] == inf {
                            continue;
                        }
                        for i in 0..self.g[v].len() {
                            let e = &self.g[v][i];
                            if e.cap > 0 && dist[e.to] > dist[v] + e.cost {
                                dist[e.to] = dist[v] + e.cost;
                                prevv[e.to] = v;
                                preve[e.to] = i;
                                update = true;
                            }
                        }
                    }
                }

                if dist[t] == inf {
                    return None;
                }

                let mut actual_flow = f;

                let mut u = t;
                loop {
                    if u == s {
                        break;
                    }
                    actual_flow = std::cmp::min(actual_flow, self.g[prevv[u]][preve[u]].cap);
                    u = prevv[u];
                }

                f -= actual_flow;
                res += actual_flow * dist[t];

                let mut u = t;
                loop {
                    if u == s {
                        break;
                    }
                    let e = self.g[prevv[u]][preve[u]].clone();
                    self.g[prevv[u]][preve[u]].cap -= actual_flow;
                    self.g[u][e.rev].cap += actual_flow;
                    u = prevv[u];
                }
            }
            
            Some(res)
        }
    }
}


Measured memory_bytes: 9092