/*
唐代杜荀鹤
《小松》
自小刺头深草里,而今渐觉出蓬蒿。
时人不识凌云木,直待凌云始道高。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <set>
#define LOCAL
const int INF = 0x7fffffff;
const int MAXN = + ;
const int maxnode = * + * ;
const int MAXM = + ;
const int MAX = ;
using namespace std;
struct Edge{
int u, v, c, f;
void init(int a, int b, int d, int e){
u = a;v = b;
c = d;f = e;
}
}edges[MAXM];
int next[MAXN], M, cur[MAXN], vis[MAXN];
int dist[MAXN], head[MAXN], n, m, sta, end; void addEdge(int u, int v, int c){
edges[M].init(u, v, c, );M++;
edges[M].init(v, u, , );//反向边
next[M - ] = head[u];
head[u] = M - ;
next[M] = head[v];
head[v] = M++;
return;
}
void init(){
M = ;//总边数
memset(head, -, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++){
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
addEdge(u, v, c);
}
sta = ;
end = n;
}
bool bfs(){
memset(vis, , sizeof(vis));
queue<int>Q;
dist[sta] = ;
vis[sta] = ;
Q.push(sta);
while (!Q.empty()){
int u = Q.front();
Q.pop();
for (int i = head[u]; i != -; i = next[i]){
int e = i, v = edges[e].v;
if (vis[v]) continue;
if (edges[e].c > edges[e].f){
vis[v] = ;
dist[v] = dist[u] + ;
Q.push(v);
}
}
}
return vis[end];
}
int dfs(int x, int a){
if (x == end || a == ) return a;
int flow = , f;
if (cur[x] == -) cur[x] = head[x];
for (int &i = cur[x]; i != -; i = next[i]){
int e = i, v = edges[i].v;
if (dist[v] == dist[x] + && (f = dfs(v, min(edges[e].c - edges[e].f, a))) > ){
flow += f;
a -= f;
edges[e].f += f;
edges[e ^ ].f -= f;
if (a == ) break;
}
}
return flow;
}
int Dinic(){
int flow = ;
while (bfs()){
memset(cur, -, sizeof(cur));
flow += dfs(sta, INF);
}
return flow;
} int main(){
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
init();
printf("%d", Dinic());
//printf("%d", (a == c));
return ;
}