#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <list>
#include <deque>
#include <set>
#include <vector>
#include <map>
#define LL long long
#define max(a,b) (a>b?a:b)
using namespace std;
const int MAXN=1e4+7;
const int MAX=0x7fffffff;
int Map[220][220];
//bool vis[MAXN];
struct node
{
int nex,val;
};
vector<int>Edge[500];
//vector<int>Edge1[MAXN];
/*
int dfs(int s,int t,int f)
{
if(s==t)
return f;
vis[s]=true;
for(int i=0;i<Edge[s].size();i++)
{
int nex=Edge[s][i];
if(!vis[nex]&&Map[s][nex]>0)
{
int d =dfs(nex,t,min(f,Map[s][nex]));
if(d>0)
{
Map[s][nex]-=d;
Map[nex][s]+=d;
return d;
}
}
}
return 0;
}
int solve(int s,int t)
{
int flow=0;
while(1)
{
memset(vis,0,sizeof(vis));
int f=dfs(s,t,MAXN);
if(f==0)return flow;
flow+=f;
}
}
*/
int n,m;
queue<int>Q;
int pre[4040];
int flow[4040];
int vis[220][220];
int bfs(int s,int t)
{
//if(s==t)return MAXN;
while(!Q.empty())
Q.pop();
for(int i=0;i<=max(n,m);i++)
{
pre[i]=-1;
}
pre[s]=0;
flow[s]=MAX;
Q.push(s);
while(!Q.empty())
{
int now=Q.front();
Q.pop();
// cout<<"flow1= "<<flow[now]<<endl;
if(now==t)
{
break;
}
/* for(int i=1;i<=m;i++)
{
if(i!=s&&Map[now][i]>0&&pre[i]==-1)
{
pre[i]=now;
flow[i]=min(flow[now],Map[now][i]);
Q.push(i);
}
}*/
for(int i=0;i<Edge[now].size();i++)
{
int nex=Edge[now][i];
if(Map[now][nex]>0&&pre[nex]==-1&&nex!=s)
{
pre[nex]=now;
flow[nex]=min(flow[now],Map[now][nex]);
// cout<<"now flow= "<<flow[nex]<<endl;
Q.push(nex);
}
}
}
// cout<<pre[t]<<endl;
// cout<<flow[t]<<endl;
if(pre[t]!=-1)
return flow[t];
else return -1;
}
LL solve(int s,int t)
{
LL ans=0;
int f=bfs(s,t);
while(f!=-1)
{
int to=t;
// cout<<"f= "<<f<<endl;
while(to!=s)
{
// cout<<pre[to]<<endl;
int fr=pre[to];
Map[fr][to]-=f;
Map[to][fr]+=f;
to=fr;
}
ans+=f;
f=bfs(s,t);
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m)
{
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
{
vis[i][j]=0;
}
for(int i=0;i<=max(n,m);i++)
Edge[i].clear(),flow[i]=MAX;
int fr,to,val;
memset(Map,0,sizeof(Map));
for(int i=0;i<n;i++)
{
cin>>fr>>to>>val;
if(fr==to)continue;
if(!vis[fr][to])
{
Edge[fr].push_back(to);
vis[fr][to]=1;
}
if(!vis[to][fr])
{
Edge[to].push_back(fr);
vis[to][fr]=1;
}
// Edge[to].push_back(fr);
Map[fr][to]+=val;
//Map[to][fr]=0;
}
cout<<solve(1,m)<<endl;
}
return 0;
}