#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<algorithm> #define For(i,j,k) for (i=j;i<=k;i++) #define reload y=q[i].x,x=q[q[i].fa].x using namespace std; const int dmax=2100,maxn=210,INF=1000000000; int c[maxn][maxn],f[maxn][maxn]; bool p[dmax]; struct node{ int x; int fa; }; struct node q[dmax]; int main(){ int i,j,k,m,n,x,y,z; freopen("ditch.in","r",stdin); freopen("ditch.out","w",stdout); scanf("%d%d",&m,&n); For(i,1,m){ scanf("%d%d%d",&x,&y,&z); c[x][y]+=z; } int flag=1,s,t; while (flag){ flag=0; s=0,t=1; memset(p,0,sizeof(p)); q[t].x=1; q[t].fa=0; p[1]=1; while (s<t){ s++; k=q[s].x; For(i,1,n) if ((c[k][i]>f[k][i] || f[i][k]>0) && !p[i]){ q[++t].x=i; q[t].fa=s; p[i]=1; if (i==n){ flag=1; break; } } if (flag) break; } if (!flag) break; int min=INF; i=t; while (q[i].fa!=0){ reload; if (c[x][y]-f[x][y]<min && c[x][y]>f[x][y]) min=c[x][y]-f[x][y]; else if (f[y][x]>0 && f[y][x]<min) min=f[y][x]; i=q[i].fa; } i=t; while (q[i].fa!=0){ reload; if (c[x][y]>f[x][y]) f[x][y]+=min; else f[y][x]-=min; i=q[i].fa; } } int ans=0; For(i,2,n) ans+=f[1][i]; printf("%d\n",ans); return 0; }