#[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,
        ES:[(usize,usize,u64);E],
    }
    let mut network = Network::new(V);
    for (u,v,cap) in ES {
        network.add_edge(u,v,cap);
    }
    println!("{}",network.max_flow(0, V-1));

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

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

    impl Network {
        fn new(n: usize) -> Network {
            Network {
                g: vec![vec![]; n],
                used: vec![false; n],
            }
        }

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

        fn add_edge(&mut self, from: usize, to: usize, cap: u64) {
            let from_rev = self.g[to].len();
            let to_rev = self.g[from].len();

            self.g[from].push(Edge {
                to: to,
                cap: cap,
                rev: from_rev,
            });
            self.g[to].push(Edge {
                to: from,
                cap: 0,
                rev: to_rev,
            });
        }

        fn dfs(&mut self, v: usize, t: usize, f: u64) -> u64 {
            if v == t {
                return f;
            }
            self.used[v] = true;
            for i in 0..self.g[v].len() {
                let e = self.g[v][i].clone();
                if !self.used[e.to] && e.cap > 0 {
                    let d = self.dfs(e.to, t, std::cmp::min(f, e.cap));
                    if d > 0 {
                        self.g[v][i].cap -= d;
                        self.g[e.to][e.rev].cap += d;
                        return d;
                    }
                }
            }
            return 0;
        }

        fn max_flow(&mut self, s: usize, t: usize) -> u64 {
            let mut flow = 0;
            loop {
                self.used = vec![false; self.n()];
                let f = self.dfs(s, t, 2_000_000_001);
                if f == 0 {
                    return flow;
                }
                flow += f;
            }
        }
    }
}
fn solve1() {
    input!{
        V:usize,E:usize,
        ES:[(usize,usize,u64);E],
    }
    let mut network = Network::new(V);
    for (u,v,cap) in ES {
        network.add_edge(u,v,cap);
    }
    println!("{}",network.max_flow(0, V-1));

    #[derive(Clone)]
    struct Edge {
        to: usize,
        cap: u64,
        rev: usize,
    }
    struct Network {
        g: Vec<Vec<Edge>>,
        level: Vec<Option<usize>>,
        iter: Vec<usize>,
    }
    impl Network {
        fn new(n: usize) -> Network {
            Network {
                g: vec![vec![]; n],
                level: vec![None; n],
                iter: vec![0; n],
            }
        }
        fn add_edge(&mut self, from: usize, to: usize, cap: u64) {
            let from_rev = self.g[to].len();
            let to_rev = self.g[from].len();
            self.g[from].push( Edge { to: to, cap: cap, rev: from_rev } );
            self.g[to].push( Edge { to: from, cap: 0, rev: to_rev } );
        }
        fn n(&self) -> usize {
            self.g.len()
        }
        fn bfs(&mut self, s: usize) {
            self.level = vec![None; self.n()];
            let mut q = std::collections::VecDeque::new();
            q.push_back(s);
            self.level[s] = Some(0);
            while let Some(v) = q.pop_front() {
                for e in &self.g[v] {
                    if e.cap > 0 && self.level[e.to].is_none() {
                        self.level[e.to] = self.level[v].map(|x| x + 1);
                        q.push_back(e.to);
                    }
                }
            }
        }
        fn dfs(&mut self, v: usize, t: usize, f: u64) -> u64 {
            if v == t {
                return f;
            }
            let iter_v_cur = self.iter[v];
            for i in iter_v_cur .. self.g[v].len() {
                let e = self.g[v][i].clone();
                if e.cap > 0 && self.level[v] < self.level[e.to] {
                    let d = self.dfs(e.to, t, std::cmp::min(f, e.cap));
                    if d > 0 {
                        self.g[v][i].cap -= d;
                        self.g[e.to][e.rev].cap += d;
                        return d;
                    }
                }
                self.iter[v] += 1;
            }
            return 0;
        }
        fn max_flow(&mut self, s: usize, t: usize) -> u64 {
            let mut flow = 0;
            loop {
                self.bfs(s);
                // finally if we could not find any path to t then return flow
                if self.level[t].is_none() {
                    return flow;
                }

                let INF = 2_000_000_001;
                self.iter = vec![0; self.n()];
                let mut f = self.dfs(s, t, INF);
                while f > 0 {
                    flow += f;
                    f = self.dfs(s, t, INF);
                }
            }
        }
    }
}


Measured memory_bytes: 9084