#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
#define pb push_back
#define mp make_pair
#define inf 1e9
#define N 501
#define L 1000000
using namespace std;
typedef long long LL;
typedef long double LD;
int len=1,pre[N],last[N],e[N],ca[N],vis[N],dis[N],q[L+1000],n,m,source,target;
void add(int x,int y,int CA)
{
pre[++len]=last[x];last[x]=len;e[len]=y;ca[len]=CA;
pre[++len]=last[y];last[y]=len;e[len]=x;ca[len]=0;
}
int Nxt(int x){if (x==L-1) return 1;return x+1;}
void bfs()
{
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
vis[source]=1;
int h=0,t=1;q[1]=source;
while (h!=t)
{
h=Nxt(h);int u=q[h];
for(int p=last[u];p;p=pre[p])
{
int rm=ca[p],v=e[p];
if (rm&&!vis[v])
{
t=Nxt(t);
q[t]=v;
dis[v]=dis[u]+1;
vis[v]=1;
}
}
}
}
int dfs(int u,int delta)
{
if (u==target) return delta;
int __res=0;
for(int p=last[u];p&¦Äp=pre[p])
{
int v=e[p],rm=ca[p];
if (rm&&dis[v]==dis[u]+1)
{
int dd=dfs(v,min(rm,delta));
ca[p]-=dd;
ca[p^1]+=dd;
delta-=dd;
__res+=dd;
}
}
return __res;
}
int MF()
{
int _res=0;
while (1)
{
bfs();
if (!vis[target]) return _res;
_res+=dfs(source,inf);
}
}
int main()
{
scanf("%d%d",&m,&n);int i,t1,t2,t3;
FOR(i,1,m)
{
scanf("%d%d%d",&t1,&t2,&t3);
add(t1,t2,t3);
}
source=1;target=n;
int ans=MF();
printf("%d\n",ans);
return 0;
}