#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 {
    int flow, capacity;
    int u, v;

    Edge(int flow, int capacity, int u, int v) {
        this->flow = flow;
        this->capacity = capacity;
        this->u = u;
        this->v = v;
    }
};

struct Vertex {
    int h, e_flow;
    Vertex(int h, int e_flow) {
        this->h = h;
        this->e_flow = e_flow;
    }
};

class Graph {
public:
    int V; // No. of vertices
    vector<Vertex> ver;
    vector<Edge> edge;

    Graph(int V) {
        this->V = V;

        // all vertices are initialized with 0 height
        // and 0 excess flow
        for (int i = 0; i < V; i++)
            ver.push_back(Vertex(0, 0));
    }

    void addEdge(int u, int v, int capacity) {
        edge.push_back(Edge(0, capacity, u, v));
    }

    void preflow(int s) {
        // Making h of source Vertex equal to no. of vertices
        // Height of other vertices is 0.
        ver[s].h = ver.size();

        //
        for (int i = 0; i < edge.size(); i++) {
            // If current edge goes from source
            if (edge[i].u == s) {
                // Flow is equal to capacity
                edge[i].flow = edge[i].capacity;

                // Initialize excess flow for adjacent v
                ver[edge[i].v].e_flow += edge[i].flow;

                // Add an edge from v to s in residual graph with
                // capacity equal to 0
                edge.push_back(Edge(-edge[i].flow, 0, edge[i].v, s));
            }
        }
    }

    // returns index of overflowing Vertex
    int overFlowVertex(vector<Vertex> &ver) {
        for (int i = 1; i < ver.size() - 1; i++)
            if (ver[i].e_flow > 0)
                return i;

        // -1 if no overflowing Vertex
        return -1;
    }

    // Update reverse flow for flow added on ith Edge
    void updateReverseEdgeFlow(int i, int flow) {
        int u = edge[i].v, v = edge[i].u;

        for (int j = 0; j < edge.size(); j++) {
            if (edge[j].v == v && edge[j].u == u) {
                edge[j].flow -= flow;
                return;
            }
        }

        // adding reverse Edge in residual graph
        Edge e = Edge(0, flow, u, v);
        edge.push_back(e);
    }

    // To push flow from overflowing vertex u
    bool push(int u) {
        // Traverse through all edges to find an adjacent (of u)
        // to which flow can be pushed
        for (int i = 0; i < edge.size(); i++) {
            // Checks u of current edge is same as given
            // overflowing vertex
            if (edge[i].u == u) {
                // if flow is equal to capacity then no push
                // is possible
                if (edge[i].flow == edge[i].capacity)
                    continue;

                // Push is only possible if height of adjacent
                // is smaller than height of overflowing vertex
                if (ver[u].h > ver[edge[i].v].h) {
                    // Flow to be pushed is equal to minimum of
                    // remaining flow on edge and excess flow.
                    int flow = min(edge[i].capacity - edge[i].flow,
                                   ver[u].e_flow);

                    // Reduce excess flow for overflowing vertex
                    ver[u].e_flow -= flow;

                    // Increase excess flow for adjacent
                    ver[edge[i].v].e_flow += flow;

                    // Add residual flow (With capacity 0 and negative
                    // flow)
                    edge[i].flow += flow;

                    updateReverseEdgeFlow(i, flow);

                    return true;
                }
            }
        }
        return false;
    }

    // function to relabel vertex u
    void relabel(int u) {
        // Initialize minimum height of an adjacent
        int mh = INT_MAX;

        // Find the adjacent with minimum height
        for (int i = 0; i < edge.size(); i++) {
            if (edge[i].u == u) {
                // if flow is equal to capacity then no
                // relabeling
                if (edge[i].flow == edge[i].capacity)
                    continue;

                // Update minimum height
                if (ver[edge[i].v].h < mh) {
                    mh = ver[edge[i].v].h;

                    // updating height of u
                    ver[u].h = mh + 1;
                }
            }
        }
    }

    // main function for printing maximum flow of graph
    int maxFlow(int s, int t) {
        preflow(s);

        // loop untill none of the Vertex is in overflow
        while (overFlowVertex(ver) != -1) {
            int u = overFlowVertex(ver);
            if (!push(u))
                relabel(u);
        }

        // ver.back() returns last Vertex, whose
        // e_flow will be final maximum flow
        return ver.back().e_flow;
    }
};

// Driver program to test above functions
int main() {
    int V, E;
    cin >> V >> E;
    Graph ff(V);
    for (int i = 0; i < E; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        ff.addEdge(u, v, c);
    }

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



Measured memory_bytes: 3208