#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template<class T> ostream &operator << (ostream &os, const vector<T> &);
template<size_t n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, const tuple<T...> &){}
template<size_t n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream &os, const tuple<T...> &t){
    os << (n == 0 ? "" : " ") << get<n>(t); _ot<n+1>(os, t);
}
template<class...T> ostream &operator << (ostream &os, const tuple<T...> &t){
    _ot<0>(os, t); return os;
}
template<class T, class U> ostream & operator << (ostream &os, const pair<T,U> &p){
    return os << "(" << p.first << ", " << p.second << ") ";
}
template<class T> ostream &operator << (ostream &os, const vector<T> &v){
    for(size_t i = 0; i < v.size(); i++) os << v[i] << (i + 1 == v.size() ? "" : ", "); return os;
}
#ifdef DEBUG
#define dump(...) (cerr << #__VA_ARGS__ << " = " << make_tuple(__VA_ARGS__) \
                   << " (L : " << __LINE__ << ")" << endl)
#else
#define dump(...)
#endif
#define all(c) begin(c), end(c)
#define range(i,a,b) for(int i = a; i < (int)(b); i++)
#define rep(i,b) range(i,0,b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
using ll = long long;
// #define int ll
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int,int>;
void fastios(){ ios_base::sync_with_stdio(0); cin.tie(0); }
int const mod = 1000000007;
auto const inf = numeric_limits<int>::max()/8;

using Capacity = int;
struct Edge {
    int src, dst;
    Capacity cap, flow;
    int rev;
    Edge(int s, int d, Capacity c = 0, Capacity f = 0, int r = 0)
        : src(s), dst(d), cap(c), flow(f), rev(r) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Capacity>;
using Matrix = vector<Capacity>;


struct Dinic {
    Graph g;
    int n, s, t;
    vector<int> level, prog, que;
    static const Capacity inf = numeric_limits<Capacity>::max()/8;

    Dinic(const Graph &g_) : n(g_.size()) {
        g.assign(n, {});
        for(auto &es : g_){
            for(auto &e : es){
                int u = e.src, v = e.dst;
                Capacity c = e.cap;
                if (u == v) continue;
                int i = g[v].size(), j = g[u].size();
                g[u].emplace_back(u,v,c,0,i);
                g[v].emplace_back(v,u,c,c,j);
            }
        }
    }
    Capacity solve(int s_, int t_){
        this->t = t_, this->s = s_;
        que.resize(n+1);
        Capacity res = 0;
        while(levelize()){ prog.assign(n, 0); res += augment(s, inf); }
        return res;
    }
    bool levelize(){
        int l = 0, r = 0;
        level.assign(n, -1); level[s] = 0; que[r++] = s;
        while(l != r){
            int v = que[l++]; if(v == t) break;
            for(auto &e : g[v]){
                int d = e.dst;
                if(level[d] == -1 && residue(e) != 0){
                    level[d] = level[v] + 1; que[r++] = d;
                }
            }
        }
        return level[t] != -1;
    }
    Capacity augment(int v, Capacity lim){
        Capacity res = 0;
        if(v == t) return lim;
        for(int &i = prog[v]; i < (int)g[v].size(); i++){
            auto &e = g[v][i];
            int d = e.dst;
            if(residue(e) == 0 || level[v] >= level[d]) continue;
            Capacity aug = augment(d, min(lim, residue(e)));
            e.flow += aug;
            reverse(e).flow -= aug;
            res += aug; lim -= aug;
            if(lim == 0) break;
        }
        return res;
    }
    inline Capacity residue(const Edge& e){ return e.cap - e.flow; }
    inline Edge &reverse(const Edge &e){ return g[e.dst][e.rev]; }
};

int main(){
    int N,M;
    cin >> N >> M;
    Graph g(N);
    rep(i,M){
        int a,b,c;
        cin >> a >> b >> c;
        g[a].emplace_back(a,b,c);
    }
    Dinic ek(g);
    cout << ek.solve(0,N-1) << endl;
}

Measured memory_bytes: 3188