#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define INF 0x7ffffff;
#define DBG printf("this is a input\n")
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
int n , m;
int w[205][205], flow[205], pre[205], ans;
int path[205] ,path_cnt;
queue <int> q;
int EKbfs(int s, int e)
{
while(!q.empty()) q.pop();
mem(pre, -1);
pre[s] = 0;
q.push(s);
flow[s] = INF; //源点拥有无限流
while(!q.empty())
{
int root = q.front();
q.pop();
if(root == e) break;
for(int i = 1 ; i <= n ; i ++)
{
if(w[root][i] > 0 && pre[i] == -1)
{
pre[i] = root;
flow[i] = min(flow[root],w[root][i]);
q.push(i);
}
}
}
if(pre[e] == -1)
return -1;
else
return flow[e];
}
void EK(int s, int e) //s为起点,e为终点
{
int add = 0;
while((add = EKbfs(s,e)) != -1)
{
path_cnt = 0;
int pp = e;
while(pp != 0)
{
int v = pre[pp];
path[path_cnt ++] = pp;
pp = v;
}
path_cnt -= 1;
for(int i = path_cnt ; i >= 1 ; i --)
{
w[path[i]][path[i-1]] -= add;
w[path[i-1]][path[i]] += add;
}
ans += add;
}
}
int main(void)
{
while(scanf("%d %d",&m,&n) != EOF) {
ans = 0;
mem(w,0);
for (int i = 1; i <= m; i++) {
int f, t, ww;
scanf("%d %d %d", &f, &t, &ww);
w[f][t] += ww;
}
EK(1, n);
printf("%d\n", ans);
}
return 0;
}