#include <fstream>
#include <climits>
#include <vector>
#include <queue>
#include <cstring>
#define NMAX 1005
#define INF INT_MAX
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
vector<int> G[NMAX];
int parent[NMAX], flow[NMAX][NMAX];
int n, m;
bool viz[NMAX];
void read_data(){
f >> n >> m;
for(int i = 1; i<=m; i++){
int x, y, c;
f >> x >> y >> c;
G[x].push_back(y);
G[y].push_back(x);
flow[x][y] += c;
}
}
bool bfs(int src, int dest){
memset(viz, false, sizeof(viz));
memset(parent, 0, sizeof(parent));
queue<int> q;
q.push(src);
while(!q.empty()){
int nod = q.front();
q.pop();
if(nod == dest){
continue;
}
for(const auto & adj : G[nod]){
if(!viz[adj] && flow[nod][adj] > 0){
parent[adj] = nod;
q.push(adj);
viz[adj] = true;
}
}
}
return viz[dest];
}
int get_flow(int src, int dest){
int max_flow = 0, min_cap;
//g << bfs(src, dest);
while(bfs(src, dest)){
for(const auto& adj : G[dest]){
if(!viz[adj] || flow[adj][dest] <=0){
continue;
}
parent[dest] = adj;
min_cap = INF;
for(int i = dest; i != src; i = parent[i]){
min_cap = min(min_cap, flow[parent[i]][i]);
}
for(int i = dest; i != src; i = parent[i]){
flow[parent[i]][i] -= min_cap;
flow[i][parent[i]] += min_cap;
}
max_flow += min_cap;
}
}
return max_flow;
}
int main(){
read_data();
g << get_flow(1, n);
return 0;
}