/* ID: shenxyy1 PROG: ditch LANG: C++ */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<algorithm> #define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)<(y)?(x):(y)) #define For(i,j,k) for (i=j;i<=k;i++) using namespace std; const int dmax=1010,INF=1000000000; int a[dmax][dmax],q[dmax*dmax],d[dmax*dmax],m,n; bool bfs(){ int f=0,l=1,i,j,k; memset(d,-1,sizeof(d)); q[1]=1; d[1]=0; while (f<l){ k=q[++f]; For(i,1,n) if (d[i]==-1 && a[k][i]>0){ d[i]=d[k]+1; q[++l]=i; } } return d[n]>0; } int dfs(int x,int low){ int i,k; if (x==n) return low; For(i,1,n) if (d[x]+1==d[i] && a[x][i]>0 && (k=dfs(i,min(low,a[x][i])))){ a[x][i]-=k; a[i][x]+=k; return k; } return 0; } int main(){ int i,j,k,x,y,z; freopen("ditch.in","r",stdin); freopen("ditch.out","w",stdout); scanf("%d%d",&m,&n); for (i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&z); a[x][y]+=z; } int ans=0; while (bfs()){ while (k=dfs(1,INF)) ans+=k; } printf("%d\n",ans); return 0; }