// Bellman-Ford - O(n*m)
#include <iostream>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cctype>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <set>
#include <string>
#include <cstring>
#include <algorithm>
#include <deque>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
typedef pair <int,int>iPair;
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,S,t,x,y,z,ok,nr,C,xc,yc,ans,Min,w,v,weight,u,poz,cost,Max,MOD,val,lvl,MAX,current,st,dr,BASE,mid,semn,T,check,nr0,nrimp,nrpar,block,INF;
vector<iPair>graf[1005];
int capacity[1005][1005];
int bfs(int s,int t,vector<int>& parent){
for(int i=0;i<=n;i++){
parent[i]=-1;
}
parent[s]=-2;
queue<iPair>Q;
Q.push({s,INF});
while(!Q.empty()){
int curr=Q.front().first;
int flow=Q.front().second;
Q.pop();
for(auto next:graf[curr]){
if(parent[next.first]==-1 && capacity[curr][next.first]){
parent[next.first]=curr;
int newflow=min(flow,capacity[curr][next.first]);
if(next.first==t) {return newflow;}
Q.push({next.first,newflow});
}
}
}
return 0;
}
int MaxFlow(int s,int t){
int flow=0;
vector<int> parent(n+5);
int newflow;
while(newflow = bfs(s,t,parent)){
flow=flow+newflow;
int curr=t;
while(curr !=s){
int prev=parent[curr];
capacity[prev][curr] = capacity[prev][curr]-newflow;
capacity[curr][prev] = capacity[curr][prev]+newflow;
curr=prev;
}
}
return flow;
}
int main()
{
in>>n>>m;
INF=999999999;
for(i=1;i<=m;i++){
in>>x>>y>>z;
graf[x].push_back({y,z});
graf[y].push_back({x,0});
capacity[x][y]=z;
capacity[y][x]=0;
}
s=1;
t=n;
out<<MaxFlow(s,t);
return 0;
}