/**
 * @brief  ?????§???????????????????????????Ford-Fulkerson????????±???
 * @note   ??¢??£URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_A
 * @date   2016/02/24 ~ 2016/03/28
 */



//****************************************
// ???????????????????????????????????????????????????
//****************************************

#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>



//****************************************
// ???????????§????????¢?????????????????????
//****************************************

#define GRAPH_BEGIN namespace graph {
#define GRAPH_END   }



//****************************************
// ????????????????§????
//****************************************

GRAPH_BEGIN



//****************************************
// ???????????????
//****************************************

using weight_t   = std::int32_t;  /**< ???(u, v)????????????w?????¨?????? */
using index_t    = std::int32_t;  /**< ??????v??????????????¨??????       */
using capacity_t = weight_t;      /**< ???(u, v)??????????????¨??????    */
using bool_t     = std::int32_t;  /**< ?????????????????´??°?????§???{ false 0 true ????????\??? }??¨?????? */



//****************************************
// ?§????????????????
//****************************************

/**
 * @brief  ????????????????§???????(scoped enum)
 * @detail (u, v) ??? E??§??????u??????????????°??????v?????°????????§??????
 *         ?????????????????????????????£??\????????¨?????????????????????????????§??????
 *         ??°???????????????????????£??\???????????¨???????????????????????????????????¢??????????????¨???????????????????¢?????????????
 */
enum struct vcolor : std::int32_t {
    white,  /**< ??????????????? */
    black,  /**< ??¢???????????? */
    gray,   /**< ??¢???????????? */
};

/**
 * @brief ??°????????¨?????????(??????) 
 */
struct vertex {
    union {
        weight_t d;       /**< ?§????s??????????????¢  */
        weight_t key;     /**< Prim?????¢?????´???????????????????????¨????±?????????????????????¨????????¶?????? */
    };
    index_t pi;           /**< ????????????(?????????) */
    union {
        vcolor color;     /**< ????????????        */
        bool_t  visited;  /**< ????????????????     */
    };
    // weight_t f;           /**< ??????????????°(DFS?????????????????????????????????????????¨??????????????????)     */
    vertex() : d(0), pi(0), color(vcolor::white)/*, f(0)*/ {}
};

/**
 * @brief ??°????????¨??¨??????(???)
 * @note  G = (V, E)???????????¢??°w???????????????????????°????????¨????????¨???
 *        ???(u, v) ??? E????????????w(u, v)??¨??¨?????????
 */
struct edge {
    index_t  src;   /**< ???????§????u */
    index_t  dst;   /**< ????????????v */
    union {
        weight_t w;     /**< ???(u, v)????????????(?????????) */
        capacity_t c;   /**< ???(u, v)????????? */
    };
    edge() = default;
    edge(index_t src, index_t dst) : src(src), dst(dst), w(1) {}
    edge(index_t src, index_t dst, weight_t w) : src(src), dst(dst), w(w) {}
};



namespace limits {
    enum {  // scoped??§????????????????????????????????????enum(strongly-typed enum)??§?????????
        inf = std::numeric_limits<weight_t>::max() / 3,  /**< @brief ???????????¨???????????´???????????¨????????????     */
        nil = std::numeric_limits<index_t>::min() / 3,   /**< @brief ?????????????????¨???????????´???????????¨???????????? */
    };
}



//****************************************
// ???????????????
//****************************************

using edges_t    = std::vector<edge>;      /**< ??°??????G=(V, E)????????????E   */
using vertices_t = std::vector<vertex>;    /**< ??°??????G=(V, E)???????????????V */
using array_t    = std::vector<weight_t>;  /**< ??????w?????????  */
using indices_t  = std::vector<index_t>;   /**< ????????????????????? */
using stamps_t   = std::vector<bool_t>;    /**< ???????????¢???????????? */
using matrix_t   = std::vector<array_t>;   /**< ??°??????G?????£??\????????¨???(???????????¨????????¨???) */
using graph_t    = std::vector<edges_t>;   /**< ??°??????G?????£??\???????????¨???(???????????????????????¨??????) */



//****************************************
// ?§????????????????
//****************************************

/**
 * @brief   ?????¬Ford-Fullkerson??¢?????´??????????????±???
 *
 * @details FORD-FULKERSON-METHOD(G, s, t)
 *          1  ?????????f???0??????????????????
 *          2  while ?¢????????????????p???????????????????????????Gf????????¨??????
 *          3    ?????????f???p????????£????¢???????
 *          4  return f
 *
 *          ?????§?????????FORD-FULKERSON-METHOD??????????????????????±?????????????????????\??????FORD-FULKERSON??¢?????´???????????§??????
 *
 *          FORD-FULKERSON(G, s, t)
 *          1  for ??????(u, v) ??? G.E
 *          2      (u, v).f = 0
 *          3      (v, u).f = 0
 *          4  while ????????????????????????Gf???s??????t?????????p????????¨??????
 *          5      cf(p) = min{cf(u, v) : (u, v)???p????±??????? }
 *          6      for ??????(u, v) in p
 *          7          (u, v).f = (u, v).f + cf(p)
 *          8          (v, u).f = (v, u).f - cf(p)
 *
 *          ?¬¬1~3????????§?????????f???0??????????????????. ?¬¬4~8??????while?????§???Gf???????¢????????????????p???????????????p????????£????????????f???????????????cf(p)???????¢??????????????????°?????????
 *          ???p?????????????????????????????????????????????????????????????????????????????§??????. ?¬¬6~8?????§??????????????????????????´??°??????. ?¢??????????????????????????????°????????????f????????§???????????§??????
 */
struct ford_fulkerson {
    stamps_t visited;             /**< ?????§????¨???????????????? */
    matrix_t c, f;                /**< ???(u, v) ??? E??????????±???§(u, v).c??¨??????????±???§(u, v).f */
    std::vector<indices_t> Gf;    /**< ????????????????????????Gf */
    index_t n;                    /**< ??????v ??? V?????° */
    capacity_t augment;           /**< ?????????????¢??????° */

    explicit ford_fulkerson(std::size_t size) : c(size, array_t(size)), f(size, array_t(size)),
                                   Gf(size, indices_t()), n(size), augment(0) {}
    explicit ford_fulkerson(const graph_t& G)
    {
        *this = ford_fulkerson(G.size());
        for (index_t i = 0; i < n; ++i) {
            for (auto&& e : G[i]) { add_edge(e.src, e.dst, e.c); }
        }
    }
    /**
     * @brief  ??????c??????????????????f??????????????¨????????????????????????Gf???????????¨???
     * @note   ??\??£s??¨?????£t???????????????????????????????????????G = (V, E)??¨??????. f???G??????????????¨???????????????u, v ??? V????????????
     *         (u, v)???????????????(residul capacity)cf(u, v)???
     *           cf(u, v) = { c(u, v) - f(u, v)  (u, v) ??? E?????¨???
     *                        f(v, u)            (v, u) ??? E?????¨???
     *                        0                  ????????\???         }
     *         ??¨????????????. ????????????(u, v) ??? E????????°???(v, u) !??? E??????????????????????????????????????????????¨??????????????????????1????????´??????????????????
     *
     *         ???????????????????????????G = (V, E)??¨?????????f????????????????????¨??????f????????£???????°??????????????????????????????????(residual network)???
     *           Ef = { (u, v) ??? V x V : cf(u, v) > 0 }
     *         ????????£??????????????????Gf = (V, Ef)??§??????
     *         ?????????????????????????????????????????????????????????(residual edge)????????£??????????????????????????¨?????§??????
     *         Ef?????????E????????????????????????????????§????????????????????£??? |Ef| <= 2|E|??§??????
     */
    void add_edge(index_t u, index_t v, capacity_t cap)
    {
        c[u][v] = cap; f[u][v] = 0;              // c?????????f????????????
        c[v][u] = 0;   f[v][u] = 0;
        Gf[u].push_back(v); Gf[v].push_back(u);  // Gf?????????
    }

    /**
     * @brief  Ford-Fulkerson?????¢?????´????????????????????????
     * @note   ??¢?????´?????????????????????????????(E|f*|)??§???????????????????????????????????§??£??\????????????????????????????????§??¨???????????????????????(E|*f| + V^2)
     * @param  ????????????????????????????????\??£(source) s
     * @param  ???????????????????????????????????£(sink)   t
     * @return ???????????????????????????????????§?????????
     */
    capacity_t compute(index_t s, index_t t)
    {
        capacity_t flow = 0;    // execute????????????s??????t???????????§???????????¨?????????

        // NOTE : ???????????????????????????????????????FORD-FULKERSON????¬¬1~3?????????????????¨????§???????????????????????????????
        
        // NOTE : ?¢????????????????p???DFS??§???????????????FORD-FULKERSON????¬¬5~8??????????????????
        while (dfs(s, t)) {
            flow += augment;
        }
        return flow;
    }

    /**
     * @brief  ??±???????????¢?´¢?????¨??????????????????????????????Gf???s??????t?????????(?¢????????????????(augment path))p?????¢?´¢??????
     *         ?¢????????????????p????????£????????????f???????????????cf(p)???????¢???????
     * @param  index_t s ????????????????????????Gf?????????u
     * @param  index_t s ???????????????????????????????????£(sink) t
     * @param  capacity_t flow ??\??£s???????????¨??¢?´¢??????????????????u?????§???q???????????????cf(q)
     * @return capacity_t cf_p
     */
    capacity_t dfs_visit(index_t u, index_t t, capacity_t flow)
    {
        visited[u] = true;           // ?¨??????°?????????
        if(u == t) { return flow; }  // ?????£(sink)t??????????????´???????????°???????????????
        
        for(auto&& v : Gf[u]) {  // ?????????v ??? Adj[u]????????????????????§?????±???????????¢?´¢??????(u, v)?????¢?´¢??????(explore)??¨??????
            // v????????§??????????????????????????????????????????????????°???(u, v)???????????????????????????
            if(visited[v] || cf(u, v) == 0) { continue; }
            
            // ?????°??????DFS-VISIT??????????????????????????????cf(p)?????????
            capacity_t cf_p = dfs_visit(v, t, std::min(flow, cf(u, v)));
            
            if (cf_p > 0) {  // cf(p)???????????§??????????????°???
                f[u][v] += cf_p;  // ??????????????????????????????????????????????????????
                f[v][u] -= cf_p;  // ?????????????????????????????????
                return cf_p;
            }
        }
        return 0;
    }

    /**
     * @brief  ??±???????????¢?´¢?????¨??????????????????????????????Gf???s??????t?????????(?¢????????????????(augment path))p?????¢?´¢??????
     *         ?¢????????????????p????????£????????????f???????????????cf(p)???????¢???????
     * @param  index_t s ????????????????????????Gf?????????u
     * @param  index_t s ???????????????????????????????????£(sink) t
     * @return bool_t  ????????????????????????Gf???s??????t?????????(?¢????????????????(augment path))p????????¨????????????
     */
    bool_t dfs(index_t u, index_t t)
    {
        visited.assign(n, false);  // DFS-VISIT??§p?????¢?????????????¨????????????°????????????false?????????
        augment = dfs_visit(u, t, limits::inf);
        return augment > 0;        // p????????¨????????????
    }

    /**< @brief ?????????u, v ??? V????????????????????????cf(u, v)????????? */
    capacity_t cf(index_t u, index_t v) const
    {
        return c[u][v] - f[u][v];
    }
};



//****************************************
// ?????????????????????
//****************************************

GRAPH_END



//****************************************
// ??¨?????????????????????
//****************************************

int main(void)
{
    using namespace std;
    int V, E;

    cin >> V >> E;
    
    graph::graph_t G(V);
    for (int i = 0; i < E; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        G[a].emplace_back(a, b, c);
    }

    graph::ford_fulkerson ff(G);
    
    auto max_flow = ff.compute(0, V-1);

    cout << max_flow << endl;

    return 0;
}

Measured memory_bytes: 3236