#include<fstream>
#include <queue>
#include <cstring>
#define NMX 10002
using namespace std;
int tata[NMX],n,m,viz[NMX];
vector<int> l[NMX];
vector<int> lin[NMX];
int f[NMX][NMX]={0},cost[NMX][NMX]={0};
int flmax ;
int s=1;
int bfs(){
int i,x;
for(int i=0;i<=n;i++) tata[i]=viz[i]=0;
queue<int> c;
c.push(s);
viz[s]=1;
while(c.size()>0){
x=c.front();
c.pop();
for(auto y:l[x]){
if(viz[y]==0 && f[x][y]<cost[x][y]){
tata[y]=x;
if(y==n)
return 1;
c.push( y);
viz[y]=1;
}
}
for(auto y:lin[x]){
if(viz[y]==0 && f[y][x]>0){
tata[y]=-x;
if(y==n)
return 1;
c.push(y);
viz[y]=1;
}
}
}
return 0;
}
int calcIP(){
int cresc=110000;
int t=n;
while(t!=s) {
if(tata[t]>0){
if(cost[tata[t]][t]-f[tata[t]][t]<cresc)
cresc= cost[tata[t]][t]-f[tata[t]][t];
t=tata[t];
}
else {
if( f[t][-tata[t]]<cresc)
cresc= f[t][-tata[t]];
t=-tata[t];
}
}
return cresc;
}
void revizuire(int cresc){
int t=n;
while(t!=s) {
if(tata[t]>0 ){
f[tata[t]][t]+=cresc;
t=tata[t];
}
else{
f[t][-tata[t]]-=cresc;
t=-tata[t];
}
}
flmax+=cresc;
}
int main(){
ifstream fs("maxflow.in");
int i,x,y,j;
int c;
flmax=0;
fs>>n>>m;
for(i=0;i<m;i++){
fs>>x>>y>>c;
l[x].push_back(y);
lin[y].push_back(x);
cost[x][y]=c;
}
fs.close();
while(bfs()){
int cresc=calcIP();
revizuire(cresc);
}
ofstream g("maxflow.out");
g<<flmax;
g.close();
return 0;
}