#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#define INF 999999
#define M 201
using namespace std;
int n,m,pre[M],cap[M][M],flow[M][M],res[M];
int maxflow(int start,int des)
{
int maxf=0;
queue<int>q;
memset(flow,0,sizeof(flow));
while(!q.empty())q.pop();
while(true){
q.push(start);
memset(pre,0,sizeof(pre));
memset(res,0,sizeof(res));
res[start]=INF;
pre[start]=start;
while(!q.empty())
{
int temp=q.front();
q.pop();
for(int i=1;i<=n;i++)
{
if(!pre[i]&&cap[temp][i]>flow[temp][i])
{
res[i]=min(res[temp],cap[temp][i]-flow[temp][i]);//这里是res[temp] !!
pre[i]=temp;
q.push(i);
}
}
}
if(res[des]==0) return maxf;
for(int i=des;i!=start;i=pre[i])
{
flow[pre[i]][i]+=res[des];//正向流加 相当于有剩余res,就不断灌水
flow[i][pre[i]]-=res[des];//反向流减,这样就可以满足前面cap[temp][i]>flow[temp][i] 从而能够回头
}
maxf+=res[des];
}
}
int main()
{
int s,e,c;
while(cin>>m>>n)
{
memset(cap,0,sizeof(cap));
while(m--)
{
cin>>s>>e>>c;
cap[s][e]+=c;
}
cout<<maxflow(1,n)<<endl;
}
}