// #define DEBUGGING
#include <bits/stdc++.h>
#define endl '\n'
#define ALL(V) begin(V), end(V)
#define ALLR(V) rbegin(V), rend(V)
using ll = std::int64_t;
using ull = std::uint64_t;
using PLL = std::pair<ll, ll>;
using TLL = std::tuple<ll, ll, ll>;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); }
namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; }
#define mv_rec make_v(init, tail...)
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail> auto make_v(T init, size_t s, Tail... tail) { return V<decltype(mv_rec)>(s, mv_rec); }
#undef mv_rec
using namespace std;

#ifdef DEBUGGING
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

namespace graph {

using Node = ll;
using Weight = ll;
using Edge = pair<Node, Weight>;

template <bool Directed>
struct Graph : public VV<Edge> {
    using VV<Edge>::VV;

    void add_edge(Node f, Node t, Weight w = 1) {
        (*this)[f].emplace_back(t, w);
        if (!Directed) (*this)[t].emplace_back(f, w);
    }

    Graph<Directed> build_inv() const {
        Graph<Directed> ret(this->size());
        for (Node i = 0; i < this->size(); i++) {
            for (const Edge &e : (*this)[i]) {
                Node j;
                Weight w;
                tie(j, w) = e;
                if (!Directed && j < i) continue;
                ret.add_edge(j, i, w);
            }
        }

        return ret;
    }
};

}

namespace graph {

using Capacity = ll;

struct FlowEdge : public tuple<Node, Capacity, ll> {
    using tuple<Node, Capacity, ll>::tuple;

    Node& to() {
        return get<0>(*this);
    }

    const Node& to() const {
        return get<0>(*this);
    }

    Capacity& cap() {
        return get<1>(*this);
    }

    const Capacity& cap() const {
        return get<1>(*this);
    }

    ll& rev_idx() {
        return get<2>(*this);
    }

    const ll& rev_idx() const {
        return get<2>(*this);
    }
};

template <bool Directed>
struct FlowGraph : public VV<FlowEdge> {
    using VV<FlowEdge>::VV;

    void add_edge(Node f, Node t, Capacity c = 1) {
        add_edge_with_rev(f, t, c);
        if (!Directed) add_edge_with_rev(t, f, c);
    }

private:
    void add_edge_with_rev(Node f, Node t, Capacity c) {
        FlowEdge fe(t, c, (ll)((*this)[t].size()));
        (*this)[f].push_back(fe);
        FlowEdge rfe(f, Capacity(), (ll)((*this)[f].size()) - 1);
        (*this)[t].push_back(rfe);
    }
};

}


namespace flow {
    
using graph::Node;
using graph::Capacity;

template <bool Directed>
class Dinic {
    graph::FlowGraph<Directed> flow_graph;
    const graph::Capacity cinf;
    V<ll> dists;
    const ll dinf = 5e15;
    V<ssize_t> watched_idx;
    Node src, sink;

    void bfs(Node start) {
        V<ll> dists(flow_graph.size(), dinf);
        dists[start] = 0;
        queue<pair<ll, Node>> que;
        que.emplace(0, start);
        while (que.size()) {
            ll d;
            Node cur;
            tie(d, cur) = que.front();
            que.pop();
            for (const auto &e : flow_graph[cur]) {
                Node nxt;
                Capacity cap;
                tie(nxt, cap, ignore) = e;
                if (cap <= Capacity()) continue;
                if (dists[nxt] <= d + 1) continue;
                dists[nxt] = d + 1;
                que.emplace(d + 1, nxt);
            }
        }
        this->dists = move(dists);
    }

    Capacity dfs(Node cur, Node pre, Capacity flow) {
        if (cur == sink) return flow;
        for (auto idx = watched_idx[cur] + 1; idx < flow_graph[cur].size(); idx++) {
            watched_idx[cur]++;
            auto &e = flow_graph[cur][idx];
            if (dists[e.to()] <= dists[cur]) continue;
            if (e.cap() <= Capacity()) continue;
            if (e.to() == pre) continue;
            auto f = dfs(e.to(), cur, min(flow, e.cap()));
            e.cap() -= f;
            flow_graph[e.to()][e.rev_idx()].cap() += f;
            if (Capacity() < f) return f;
        }
        return Capacity();
    }

public:
    Dinic(graph::FlowGraph<Directed> flow_graph, const Capacity &cinf) :
        flow_graph(flow_graph), cinf(cinf), watched_idx(flow_graph.size()) { }

    Capacity max_flow(Node src, Node sink) {
        this->src = src;
        this->sink = sink;
        auto ret = Capacity();
        while (true) {
            bfs(src);
            if (dists[sink] == dinf) break;
            fill(ALL(watched_idx), -1);
            while (true) {
                auto tmp = dfs(src, -1, cinf);
                if (tmp == Capacity()) break;
                ret += tmp;
            }
        }
        return ret;
    }
};

}

int main() {
    const ll inf = 5e15;
    ll N, M;
    cin >> N >> M;
    graph::FlowGraph<true> graph(N);
    while (M--) {
        ll u, v, c;
        cin >> u >> v >> c;
        graph.add_edge(u, v, c);
    }

    flow::Dinic<true> d(graph, inf);
    cout << d.max_flow(0, N - 1) << endl;
    return 0;
}



Measured memory_bytes: 3280