#include<cstdio>
#include<algorithm>
#define N 201
#define INF 10000000
using namespace std;
int n,m,tot=1,front[N],ans,lev[N],src,dec,cur[N],que[N],head,tail;
struct node
{
int next,to,cap;
}e[N*2];
inline void add(int u,int v,int w)
{
e[++tot].to=v;e[tot].next=front[u];e[tot].cap=w;front[u]=tot;
e[++tot].to=u;e[tot].next=front[v];e[tot].cap=0;front[v]=tot;
}
inline bool bfs()
{
for(int i=src;i<=dec;i++) {lev[i]=-1;cur[i]=front[i];}
head=tail=0;
que[tail++]=src;lev[src]=0;
while(head<tail)
{
for(int i=front[que[head]];i;i=e[i].next)
if(e[i].cap>0&&lev[e[i].to]==-1)
{
lev[e[i].to]=lev[que[head]]+1;
que[tail++]=e[i].to;
if(e[i].to==dec) return true;
}
head++;
}
return false;
}
inline int dinic(int u,int flow)
{
if(u==dec) return flow;
int res=0,delta;
for(int & i=cur[u];i;i=e[i].next)
{
if(e[i].cap>0&&lev[e[i].to]>lev[u])
{
delta=dinic(e[i].to,min(e[i].cap,flow-res));
if(delta)
{
e[i].cap-=delta;e[i^1].cap+=delta;
res+=delta; if(res==flow) break;
}
}
}
if(res!=flow) lev[u]=-1;
return res;
}
int main()
{
scanf("%d%d",&n,&m);
int u,v,w;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
src=1;dec=m;
while(bfs())
ans+=dinic(src,INF);
printf("%d",ans);
}