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

typedef int Capacity;
struct Edge {
    int src, dst;
    Capacity cap;
    Edge(int src_, int dst_, int cap_) :
        src(src_), dst(dst_), cap(cap_) { }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<int> Array;
typedef vector<Array> Matrix;

template<class Capacity = int> struct max_flow {
    typedef vector<vector<Capacity>> Matrix;
    size_t n;
    Matrix cap;
    vector<bool> vis;
    Capacity res;
    max_flow(const int s, const int t, const Graph &g) : n(g.size()), cap(n, Array(n)) {
        for(size_t i = 0; i < g.size(); i++){
            for(size_t j = 0; j < g[i].size(); j++){
                int u = g[i][j].src, v = g[i][j].dst;
                cap[u][v] = g[i][j].cap;
            }
        }
        res = solve(s, t);
    }
    Capacity augment(const int v, const int t, Capacity lim){
        vis[v] = true;
        if(v == t) return lim;
        for(size_t i = 0; i < n; i++){
            if(vis[i] || cap[v][i] == 0) continue;
            Capacity f = augment(i, t, min(lim, cap[v][i]));
            cap[v][i] -= f, cap[i][v] += f;
            if(f) return f;
        }
        return 0;
    }
    Capacity solve(const int s, const int t){
        Capacity res = 0, f = -1;
        while(f) vis.assign(n,false), res += (f = augment(s,t,inf));
        return res;
    }
};

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

Measured memory_bytes: 1280