#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b, c, maxfl, fl, start=1, sink, i;
vector<int> graph[1001];
int F[1001], capacitate[5001][5001];
bool viz[1001];
bool bfs() {
queue<int> q;
memset(viz, false, sizeof(viz));
for (i=1; i<=n; ++i) {
q.push(start);
F[start] =-1;
viz[start] =1;
while (!q.empty()) {
a = q.front();
q.pop();
for (int &vecin: graph[a]) {
if (!viz[vecin] && capacitate[a][vecin] != 0) {
F[vecin] = a;
if (vecin==sink)
return true;
viz[vecin] = true;
q.push(vecin);
}
}
}
}
return false;
}
void maxflow() {
while (bfs()) { //verifica pentru fiecare pas daca putem face o parcurgere BFS
a = sink;
fl = INT_MAX;
while (a != start) {
if (capacitate[F[a]][a] < fl)
fl = capacitate[F[a]][a];
a = F[a];
}
a = sink;
while (a!=start) {
capacitate[a][F[a]] += fl;
capacitate[F[a]][a] -= fl;
a = F[a];
}
maxfl += fl;
}
}
int main() {
ifstream f("maxflow.in");
ofstream g("maxflow.out");
f>>n>>m;
sink = n;
for (i=0; i<m; ++i) {
f>>a>>b>>c;
graph[a].push_back(b);
graph[b].push_back(a);
capacitate[a][b] = c;
}
maxflow();
g<<maxfl;
f.close(); g.close();
return 0;
}