#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#define pb push_back
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int N, M, cap[1002][1002], flux[1002][1002], prec[1002], x, y, c, maxflow, source, sink;
vector <int> G[1005];
queue < int > Q;
bool bfs(int sursa)
{
Q.push(sursa);
for (int i=1; i<=N; ++i)
prec[i]=0; prec[sursa]=sursa;
while (Q.size())
{
int nod=Q.front(); Q.pop();
vector <int>::iterator it=G[nod].begin();
for (; it!=G[nod].end(); ++it)
if (!prec[*it] && cap[nod][*it]>flux[nod][*it])
prec[*it]=nod, Q.push(*it);
}
return (prec[sink]!=0);
}
void Flow(int sursa)
{
while (bfs(sursa))
{
vector <int>::iterator it=G[sink].begin();
for (; it!=G[sink].end(); ++it)
{
int minim=(1<<30);
for (int x=sink; x!=source; x=prec[x])
if (cap[prec[x]][x]-flux[prec[x]][x]<minim)
minim=cap[prec[x]][x]-flux[prec[x]][x];
if (minim!=(1<<30))
{
for (int x=sink; x!=source; x=prec[x])
flux[prec[x]][x]+=minim, flux[x][prec[x]]-=minim;
maxflow+=minim;
}
}
}
}
int main()
{
f>>N>>M; source=1; sink=N;
for (int i=1; i<=M; ++i)
f>>x>>y>>c, cap[x][y]+=c, G[x].pb(y), G[y].pb(x);
Flow(source); g<<maxflow<<'\n';
return 0;
}