#include<iostream>
#include<cstring>
#include<queue>
#define ll long long
using namespace std;
#define maxn 500
int Depth[maxn + 5], Head[maxn + 5], Next[maxn + 5], V[maxn + 5], W[maxn + 5];
int cnt, s, t;
void add(int x, int y, int w)//建边
{
++cnt;
Next[cnt] = Head[x];
V[cnt] = y; W[cnt] = w;
Head[x] = cnt;
++cnt;
Next[cnt]=Head[y];
V[cnt]=x;W[cnt]=0;
Head[y]=cnt;
}
bool bfs()
{
queue<int>Q;
while (!Q.empty())Q.pop();
memset(Depth, -1, sizeof(Depth));
Depth[s] = 0;
Q.push(s);
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i = Head[u]; i != -1; i = Next[i])
{
if (W[i] > 0 && Depth[V[i]] == -1)
{
Q.push(V[i]);
Depth[V[i]] = Depth[u] + 1;
}
}
}
if (Depth[t] == -1)return 0;
return 1;
}
int dfs(int u, int flow)//求流量
{
if (u == t)
{
return flow;
}
for (int i = Head[u]; i != -1; i = Next[i])
{
if ((Depth[V[i]] == Depth[u] + 1) && (W[i] != 0))
{
int di = dfs(V[i], min(flow, W[i]));
if (di>0)
{
W[i] -= di;
W[i ^ 1] += di;
return di;
}
}
}
return 0;
}
int main()
{
int N,M;
while(cin>>N>>M)
{
cnt = -1;
memset(Head, -1, sizeof(Head));
memset(Next, -1, sizeof(Next));
s=0;t=M;
add(s,1,1000000000);
for(int i=1;i<=N;++i)
{
int u,v,w;
cin>>u>>v>>w;
add(u,v,w);
}
int ans=0;
while(bfs())ans+=dfs(s,1000000000);
cout<<ans<<'\n';
}
}