#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define inf 100000000
struct node{
int to,v,p;
}e[300];
int ans,n,m,num=1,head[300];
void add(int from,int to,int v){
e[++num].to=to;
e[num].v=v;
e[num].p=head[from];
head[from]=num;
}
int dep[300],road[300],NUM;
bool bfs(){
queue<int>q;
memset(dep,127/3,sizeof(dep));
q.push(1);
dep[1]=0;
while(!q.empty()){
int point=q.front();q.pop();
for(int i=head[point];i;i=e[i].p){
if(e[i].v&&dep[e[i].to]>dep[point]+1){
dep[e[i].to]=dep[point]+1;
if(e[i].to==n)return true;
q.push(e[i].to);
}
}
}
return false;
}
int dinic(int s,int f){
if(s==n)return f;
int rest=f;
for(int i=head[s];i;i=e[i].p){
if(e[i].v&&dep[e[i].to]==dep[s]+1&&rest){
int t=dinic(e[i].to,min(rest,e[i].v));
if(!t)dep[e[i].to]=0;
e[i].v-=t;
e[i^1].v+=t;
rest-=t;
}
}
return f-rest;
}
int main(){
scanf("%d%d",&m,&n);
int f,t,v;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&f,&t,&v);
add(f,t,v);
add(t,f,0);
}
while(bfs())
ans+=dinic(1,inf);
printf("%d",ans);
}