/**
 * @brief  ?????§?????????????±???????EdmondsKarp??¢?????´??????????????±???
 * @note   ??¢??£URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_A
 * @date   2016/03/03
 */



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

#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>
#include <type_traits>
#include <cassert>
#include <queue>



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

#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)??????????????¨??????    */



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 ?????????????????¨???????????´???????????¨???????????? */
    };
}



/**
 * @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  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) {}
};



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 matrix_t   = std::vector<array_t>;   /**< ??°??????G?????£??\????????¨???(???????????¨????????¨???) */
using graph_t    = std::vector<edges_t>;   /**< ??°??????G?????£??\???????????¨???(???????????????????????¨??????) */

using bool_t     = std::int32_t;           /**< ???????????¢?????? */
using stamps_t   = std::vector<bool_t>;    /**< ???????????¢???????????? */



/**
 * @brief Edmonds-Karp?????¢?????´?????????
 * @note  Ford-Fulkerson???????????????????¢??????????????????????????????¢?´¢?????¨????????¢?´¢???????????¨??§FORD-FULKERSON????¨??????????????????????????????§??????
 *        ???????????????????????????????????????????????§s??¨t????????¶?????????????¢??????????????????¨???????????????????????§??????. ?????????????????????????????????????????????(u, v)????????¢(??????)???1??§??????
 *        Ford-Fulkerson??????????????????????????????????????????Edmonds-Karp??¢?????´?????????(Edmonds-Karp algorithm)??¨?????¶
 */
struct edmonds_karp {
    indices_t pi;                 /**< ??????v?????????????±???§ */
    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?????° */

    explicit edmonds_karp(std::size_t size) :
        pi(size), visited(size), c(size, array_t(size)), f(size, array_t(size)),
        Gf(size, indices_t()), n(size) { }
    explicit edmonds_karp(const graph_t& G)
    {
        *this = edmonds_karp(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  Edmonds-Karp?????¢?????´????????????????????????
     * @note   ??¢?????´?????????????????????????????(VE^2)??§???????????????????????????????????§??£??\????????????????????????????????§??¨???????????????????????(VE^2 + V^2)
     * @param  index_t s ????????????????????????????????\??£(source) s
     * @param  index_t t ???????????????????????????????????£(sink) t
     * @return ???????????????????????????????????§?????????
     */
    capacity_t compute(index_t s, index_t t)
    {
        capacity_t flow = 0;  // execute????????????s??????t???????????§???????????¨?????????

        // NOTE: ???????????????????????????????????????FORD-FULKERSON????¬¬1~3?????????????????¨????§???????????????????????????????
        
        while (bfs(s, t)) { flow += proc(s, t); }  // BFS??§p?????¢??????p????????¨??????????????°?????????????????´??°??????
        return flow;
    }


    /**
     * @brief  ???????????¢?´¢?????¨??????????????????????????????Gf???s??????t?????????(?¢????????????????(augment path))p?????¢?´¢??????
     * @param  index_t s ????????????????????????????????\??£(source) s
     * @param  index_t s ???????????????????????????????????£(sink) t
     * @return ?¢????????????????p????????¨???????????????
     */
    bool_t bfs(index_t s, index_t t)
    {
        pi.assign(n, limits::nil);
        visited.assign(n, false);
        std::queue<index_t> Q;

        // ????¶????????§???¨??????????§????s?????????????????¨?????????
        pi[s] = limits::nil;  // ?§???????????????????NIL??§???????????????
        visited[s] = true;    // ?¨??????°?????????
        
        Q.push(s);  // s?????????????????????????????\?????????????????????
        while (!Q.empty()) {
            index_t u = Q.front(); Q.pop();
            for (auto&& v : Gf[u]) {
                // v????????§???????????????????????????????????????????????°??????(u, v)???????????????????????????
                if (visited[v] || cf(u, v) == 0) { continue; }
                
                // ????¨?????????¶??????????????????????????????????????´??????
                visited[v] = true;  // ?¨??????°????????????
                pi[v] = u;          // u????????????v.pi??¨???????¨??????????
                Q.push(v);          // v????????\???Q???????°??????????
            }
        }
        return visited[t];  // ??????????????????????????£??????t????¨????????????????p???????????????????????§??????
    }


    /**
     * @brief  ?¢????????????????p????????£????????????f???????????????cf(p)???????¢???????
     * @param  index_t s ????????????????????????????????\??£(source) s
     * @param  index_t t ???????????????????????????????????£(sink) t
     * @return capacity_t cf_p
     */
    capacity_t proc(index_t s, index_t t)
    {
        capacity_t cf_p = limits::inf;
        index_t v = t;

        // ?????????????¢??????????????±??????????. sink??????source?????¨?¨??????????????????§????°????????????¨?????????
        while (v != s) {
            cf_p = std::min(cf_p, cf(pi[v], v));
            v = pi[v];
        }

        // ???????????§????????????????°??????????????¢?????????????
        v = t;
        while (v != s) {
            f[pi[v]][v] += cf_p;  // ??????????????????????????????(?????????)????????????????????????
            f[v][pi[v]] -= cf_p;  // ????????????(?????????)?????????????????????
            v = pi[v];
        }
        
        return cf_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::edmonds_karp ek(G);

    auto max_flow = ek.compute(0,V-1);

    cout << max_flow << endl;

    return 0;
}

Measured memory_bytes: 3244