#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 205;
const int INF = 1 << 20;
struct edge{
int u,v,cap,flow;
edge(int u,int v,int cap,int flow) : u(u),v(v),cap(cap),flow(flow) {}
};
struct EdmonsKarp{
int e,v;
vector<int> mp[maxn];
vector<edge> es;
int path[maxn];
int aug[maxn];
void init()
{
for (int i = 1; i <= v; i ++) {
mp[i].clear();
}
es.clear();
memset(path, 0, sizeof(path));
}
void addedge(int u,int v,int c)
{
es.push_back(edge(u,v,c,0));
es.push_back(edge(v,u,0,0));
int num = es.size();
mp[u].push_back(num - 2);
mp[v].push_back(num - 1);
}
int maxflow(int s,int t)// 1 m
{
int flow = 0;
for(;;){
memset(aug, 0, sizeof(aug));
queue<int> q;
q.push(s);
aug[s] = INF;
while (!q.empty()) {//bfs
int x = q.front();
q.pop();
for (int i = 0; i < mp[x].size(); i ++) {
edge &e = es[mp[x][i]];
if(!aug[e.v] && e.cap > e.flow)
{
path[e.v] = mp[x][i];
aug[e.v] = min(aug[x],e.cap - e.flow);
q.push(e.v);
}
}
if (aug[t]) break;
}
if (!aug[t]) break;
for (int i = t; i != s; i = es[path[i]].u) {
es[path[i]].flow += aug[t];
es[path[i] ^ 1].flow -= aug[t];
}
flow += aug[t];
}
return flow;
}
};
int main()
{
int n,m;//E V
while (cin >> n >> m) {
int a,b,c;
EdmonsKarp ek;
for (int i = 0; i < n; i ++) {
scanf("%d%d%d",&a,&b,&c);
ek.addedge(a,b,c);
}
printf("%d\n",ek.maxflow(1, m));
}
return 0;
}