#include <bits/stdc++.h>
const int NMAX = 1005;
const int INF = 1e9;
struct edge {
int nod, revIdx;
int flux, capacitate;
};
std::vector<edge> G[NMAX];
int N, sursa, destinatie;
bool vis[NMAX];
int parent[NMAX], parentEdgeIdx[NMAX];
bool bfs() {
std::queue<int> q;
q.push(sursa);
std::fill(vis, vis + N, false);
vis[sursa] = true;
while (!q.empty() && !vis[destinatie]) {
int nod = q.front();
q.pop();
int i = 0;
for (const edge& e : G[nod]) {
if (e.flux < e.capacitate && !vis[e.nod]) {
vis[e.nod] = true;
parent[e.nod] = nod;
parentEdgeIdx[e.nod] = i;
q.push(e.nod);
}
i++;
}
}
return vis[destinatie];
}
int main() {
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int M, a, b, cap;
scanf("%d%d", &N, &M);
sursa = 0;
destinatie = N - 1;
for (int i = 0; i < M; i++) {
scanf("%d%d%d", &a, &b, &cap);
--a;
--b;
int idxA = G[b].size();
int idxB = G[a].size();
G[a].push_back({b, idxA, 0, cap});
G[b].push_back({a, idxB, 0, 0});
}
int totalFlow = 0;
while (bfs()) {
int minFlow = INF;
for (int i = destinatie; i != sursa; i = parent[i]) {
int edgeIdx = parentEdgeIdx[i];
int pare = parent[i];
minFlow = std::min(minFlow, G[pare][edgeIdx].capacitate - G[pare][edgeIdx].flux);
}
totalFlow += minFlow;
for (int i = destinatie; i != sursa; i = parent[i]) {
int edgeIdx = parentEdgeIdx[i];
int pare = parent[i];
G[pare][edgeIdx].flux += minFlow;
G[i][G[pare][edgeIdx].revIdx].flux -= minFlow;
}
}
printf("%d\n", totalFlow);
return 0;
}