#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;
template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
inline double distance(double y1, double x1, double y2, double x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = { 0, 1, 0, -1 }, dx = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）

using namespace std;

struct Edge {
    const int to;       // 行き先のノード番号
    const int rev;      // 逆辺のindex. graph[to][rev]でアクセスできる
    int flow;           // 流量
    const int capacity; // 容量
    const bool is_rev;  // 逆辺かどうか
    Edge(int to, int rev, int flow, int capacity, bool is_rev) : to(to), rev(rev), flow(flow), capacity(capacity), is_rev(is_rev) {}
};

struct Node {
    int height = 0;      // 高さ
    int excessFlow = 0;  // 過剰フロー
    Node(int height, int excessFlow) : height(height), excessFlow(excessFlow) {}
};

class PushRelabel {
public:
    vector<Node> nodeList;
    vector<vector<Edge>> graph;

    explicit PushRelabel(unsigned int numOfNode) {
        for (int i = 0; i < numOfNode; ++i) {
            nodeList.emplace_back(Node(0, 0));
        }
        graph.resize(numOfNode);
    }

    void addEdge(int from, int to, int capacity) {
        graph[from].emplace_back(Edge(to, (int)graph[to].size(), 0, capacity, false));
        graph[to].emplace_back(Edge(from, (int)graph[from].size() - 1, capacity, capacity, true));
    }

    // sourceからsinkへの最大流を求める
    int maxFlow(int source, int sink) {
        preflow(source);
        int node;
        while ((node = overFlowNode()) != -1) {
            if (not push(node)) {
                relabel(node);
            }
        }
        return nodeList.back().excessFlow;
    }
    
private:
    void preflow(int source) {
        nodeList[source].height = (int)nodeList.size();

        for (Edge &edge : graph[source]) {
            if (edge.is_rev) { continue; }
            edge.flow = edge.capacity;
            graph[edge.to][edge.rev].flow = 0;
            nodeList[edge.to].excessFlow += edge.flow;
        }
    }

    // オーバーフロー頂点を探す
    int overFlowNode() {
        for (int i = 1; i < nodeList.size() - 1; ++i) {
            if (nodeList[i].excessFlow > 0) {
                return i;
            }
        }
        return -1;
    }

    // fromから他のノードにpushする
    bool push(int from) {
        for (Edge &edge : graph[from]) {
            if (edge.flow == edge.capacity) { continue; }

            if (nodeList[from].height > nodeList[edge.to].height) {
                int flow = min(edge.capacity - edge.flow, nodeList[from].excessFlow);
                nodeList[from].excessFlow -= flow;
                nodeList[edge.to].excessFlow += flow;
                edge.flow += flow;
                graph[edge.to][edge.rev].flow -= flow;
                return true;
            }
        }
        return false;
    }

    // fromのheightを修正
    void relabel(int from) {
        int minHeight = INT_MAX;
        for (const Edge &edge : graph[from]) {
            if (edge.flow == edge.capacity) { continue; }
            minHeight = min(minHeight, nodeList[edge.to].height);
        }
        assert(minHeight != INT_MAX);
        nodeList[from].height = minHeight + 1;
    }
    
};

int main() {
    int V, E;
    cin >> V >> E;
    PushRelabel pr(V);
    for (int i = 0; i < E; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        pr.addEdge(u, v, c);
    }

    cout << pr.maxFlow(0, V - 1) << endl;
    return 0;
}


Measured memory_bytes: 3228