#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 220;
int G[maxn][maxn];//描绘整张图;
int flow[maxn];//从原点到当前点的流量;
int pre[maxn];//标记上一步的增广路是哪一条路;
int n, m;
queue<int>q;
int bfs(int s, int t)//找增广路
{
while (!q.empty()) q.pop();
memset(pre, -1, sizeof(pre));
pre[s] = 0, flow[s] = inf;
q.push(s);
while (!q.empty())
{
int p = q.front();q.pop();
if (p == t) break;
for (int u = 1; u <= n; u++)
{
if (u != s && G[p][u] > 0 && pre[u] == -1)
{
pre[u] = p;
flow[u] = min(flow[p], G[p][u]);
q.push(u);
}
}
}
if (pre[t] == -1) return -1;
return flow[t];
}
int EK(int s, int t)
{
int delta = 0;//当前残余网络被更新了多少。
int tot = 0;//总的最大值是多少。
while (1)
{
delta = bfs(s, t);
if (delta == -1) break;
int p = t;
while (p != s)
{
G[pre[p]][p] -= delta;
G[p][pre[p]] += delta;
p = pre[p];
}
tot += delta;
}
return tot;
}
int main()
{
while (~scanf("%d%d", &m, &n))
{
int u, v, w;
memset(G, 0, sizeof(G));
memset(flow, 0, sizeof(flow));
for (int i = 0; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
G[u][v] += w;
}
printf("%d\n", EK(1, n));
}
return 0;
}