#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n, m;
const int MAX = 1e3;
vector<int> G[MAX + 1];
int capacitate[MAX + 1][MAX + 1], flux[MAX + 1][MAX + 1];
int vis[MAX + 1], parent[MAX + 1];
int flow(int sursa, int destinatie) {
for (int i = 1; i <= n; i++) {
vis[i] = 0;
parent[i] = 0;
}
queue<int> q;
q.push(sursa);
vis[sursa] = 1;
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto vec : G[node]) {
if (!vis[vec] && capacitate[node][vec] - flux[node][vec] > 0) {
vis[vec] = 1;
parent[vec] = node;
q.push(vec);
}
}
}
if (!vis[destinatie]) {
return 0;
}
vector<int> path;
int node = destinatie;
while (node != sursa) {
path.push_back(node);
node = parent[node];
}
path.push_back(sursa);
reverse(path.begin(), path.end());
int f = 1e9;
for (int i = 0; i < path.size() - 1; i++) {
int x = path[i], y = path[i + 1];
f = min(f, capacitate[x][y] - flux[x][y]);
}
for (int i = 0; i < path.size() - 1; i++) {
int x = path[i], y = path[i + 1];
flux[x][y] += f;
flux[y][x] -= f;
}
return f;
}
int main() {
fin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v, c;
fin >> u >> v >> c;
G[u].push_back(v);
capacitate[u][v] += c;
G[v].push_back(u);
}
int sum = 0;
while (true) {
int val = flow(1, n);
if (val == 0) {
break;
}
sum += val;
}
fout << sum;
}