#include <cstdio>
#include <cstring>
#define INF 1000000000
#define MAXN 1000
int c[MAXN+1][MAXN+1], f[MAXN+1][MAXN+1], from[MAXN+1], n, q[MAXN+1];
inline void actualizez(int min){
int p=n;
while(from[p]){
f[from[p]][p]+=min;
f[p][from[p]]-=min;
p=from[p];
}
}
inline int minim(){
int p=n, ans=INF;
while(from[p]){
if(ans>c[from[p]][p]-f[from[p]][p]){
ans=c[from[p]][p]-f[from[p]][p];
}
p=from[p];
}
return ans;
}
inline bool bfs(){
int st, dr, i;
st=0;
dr=1;
q[0]=1;
memset(from, 0, sizeof from);
while(st<dr){
for(i=2; i<=n; i++){
if((from[i]==0)&&(c[q[st]][i]>f[q[st]][i])){
from[i]=q[st];
q[dr++]=i;
}
}
if(from[n]!=0){
return 1;
}
st++;
}
return 0;
}
int main(){
int m, i, x, y, z, ans, min;
FILE *fin, *fout;
fin=fopen("maxflow.in", "r");
fout=fopen("maxflow.out", "w");
fscanf(fin, "%d%d", &n, &m);
for(i=1; i<=m; i++){
fscanf(fin, "%d%d%d", &x, &y, &z);
c[x][y]=z;
}
while(bfs()){
min=minim();
actualizez(min);
}
ans=0;
for(i=1; i<n; i++){
ans+=f[i][n];
}
fprintf(fout, "%d\n", ans);
fclose(fin);
fclose(fout);
return 0;
}