#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N=505;
int cf[N][N],pre[N];
int n,m,flow[N];
int s,t;
int bfs()
{
queue<int> q;
while(!q.empty()) q.pop();
memset(pre,-1,sizeof(pre));
pre[s]=0;
flow[s]=0x3f3f3f3f;
q.push(s);
while(!q.empty())
{
int cur=q.front();
q.pop();
if(cur==t) break;
for(int i=1;i<=n;i++)
if(pre[i]==-1&&cf[cur][i])
{
flow[i]=flow[cur]<cf[cur][i]?flow[cur]:cf[cur][i];
q.push(i);
pre[i]=cur;
}
}
if(pre[t]==-1)return -1;
return flow[t];
}
int EK()
{
int max_flow=0,step,now;
while(1)
{
if((step=bfs())==-1)break;
max_flow+=step;
now=t;
while(now!=s)
{
int tmp=pre[now];
cf[tmp][now]-=step;
cf[now][tmp]+=step;
now=tmp;
}
}
return max_flow;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
cf[a][b]+=w;
}
s=1,t=n;
printf("%d\n",EK());
}