LLJ
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int inf=1000000000;
const int maxn=20000,maxm=500000;//最大点和边数
struct Edge{
int v,f,nxt;
};
int src,sink;
int g[maxn+10];
int nume;
Edge e[maxm*2+10];
void addedge(int u,int v,int c){
e[++nume].v=v;
e[nume].f=c;
e[nume].nxt=g[u];
g[u]=nume;
e[++nume].v=u;
e[nume].f=0;
e[nume].nxt=g[v];
g[v]=nume;
}
void init(){
memset(g,0,sizeof(g));
nume=1;
}
queue<int> q;
bool vis[maxn+10];
int dist[maxn+10];
void bfs(){
memset(dist,0,sizeof(dist));
while(!q.empty())
q.pop();
vis[src]=true;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=g[u];i;i=e[i].nxt)
if(e[i].f&&!vis[e[i].v]){
q.push(e[i].v);
dist[e[i].v]=dist[u]+1;
vis[e[i].v]=true;
}
}
}
int dfs(int u,int delta){
if(u==sink){
return delta;
}
else{
int ret=0;
for(int i=g[u];delta&&i;i=e[i].nxt)
if(e[i].f&&dist[e[i].v]==dist[u]+1){
int dd=dfs(e[i].v,min(e[i].f,delta));
e[i].f-=dd;
e[i^1].f+=dd;
delta-=dd;
ret+=dd;
}
return ret;
}
}
int maxflow(){
int ret=0;
while(1){
memset(vis,0,sizeof(vis));
q.push(e[src].v);
bfs();
if(!vis[sink])
return ret;
ret+=dfs(src,inf);
cout<<ret<<endl;
}
}
int n,m;
int main(){
while(cin>>n>>m){
int a,b,c;
init();
src=0;
sink=m;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
int ans=maxflow();
printf("%d\n",ans);
}
}