let rec fold_for a b v f = if a >= b then v else fold_for (a+1) b (f a v) f
let inf = max_int / 100

type edges = { dest: int; mutable cap: int; }

let max_flow start goal g =
  let n = Array.length g in

  let find_flow () =
    let visited = Array.make n false in
    let rec aux x flo =
      if x = goal then flo
      else begin
        visited.(x) <- true;
        fold_for 0 n 0 @@ fun y v ->
          let { dest=y'; cap=c } = g.(x).(y) in
          assert (y = y');
          if v > 0 || visited.(y) || c <= 0 then v
          else begin
            assert (v = 0);
            let v = aux y (min flo c) in
            if v > 0 then begin
              g.(x).(y).cap <- g.(x).(y).cap - v;
              g.(y).(x).cap <- g.(y).(x).cap + v;
            end;
            v
          end
      end
    in
    aux start inf
  in

  let rec loop ans =
    let flo = find_flow () in
    if flo = 0 then ans else loop (ans+flo)
  in loop 0

let () =
  Scanf.scanf "%d %d" @@ fun n m ->
  let g = Array.init n (fun _ -> Array.init n (fun j -> { dest = j; cap = 0 })) in
  for _ = 1 to m do
    Scanf.scanf " %d %d %d" @@ fun u v c -> g.(u).(v).cap <- c
  done;
  Printf.printf "%d\n" (max_flow 0 (n-1) g)

