#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(kbrni,n)cout<<" "<<a[kbrni];cout<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define svecp(v) cout<<#v<<":";each(kbrni,v)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

//実測はあまり速くない
//O(n^3) Goldberg and Tarjan 1988 を参照(動的木により計算量は改善可能)
template<typename T> class PushRelabel{
private:
    vector<vector<T> > cap;
    int V;
public:
    PushRelabel(int node_size){
        V = node_size;
        cap.resize(V,vector<T>(V,0));
    }
    void add_edge(int u,int v,T capacity){
        cap[u][v] = capacity;
    }
    int solve(int s,int t){
        vector<int> d(V,0),mxd(V,0);
        vector<T> ex(V,0);
        vector<vector<T> > f(V,vector<T>(V,0));
        d[s] = V-1;
        rep(i,V){
            f[s][i] = ex[i] = cap[s][i];
            f[i][s] = -cap[s][i];
        }
        for(int sz = 0;;){
            if(sz == 0){
                rep(i,V){
                    if(i != s && i != t && ex[i] > 0){
                        if(sz != 0 && d[i] > d[mxd[0]]){
                            sz = 0;
                        }
                        mxd[sz++] = i;
                    }
                }
            }
            if(sz == 0) break;
            while(sz){
                int i = mxd[sz-1];
                bool push = false;
                for(int j = 0; j < V && ex[i] != 0; j++){
                    if(d[i] == d[j] + 1 && cap[i][j] - f[i][j] > 0){
                        //push操作
                        push = true;
                        int df = min(cap[i][j] - f[i][j], ex[i]);
                        f[i][j] += df, f[j][i] -= df;
                        ex[i] -= df, ex[j] += df;
                        if(ex[i] == 0){
                            sz--;
                        }
                    }
                }
                if(!push){
                    //relabel操作
                    d[i] = numeric_limits<int>::max();
                    rep(j,V){
                        if(d[i] > d[j] + 1 && cap[i][j] - f[i][j] > 0){
                            d[i] = d[j] + 1;
                        }
                    }
                    if(d[i] > d[mxd[0]]){
                        sz = 0;
                        break;
                    }
                }
            }
        }
        int flow = 0;
        rep(i,V){
            flow += f[s][i];
        }
        return flow;
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    PushRelabel<int> pr(n);
    rep(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        pr.add_edge(a,b,c);
    }
    cout << pr.solve(0,n-1) << "\n";
    return 0;
}



Measured memory_bytes: 3288