#include<bits/stdc++.h>
using namespace std;
const int INF = 2e9;
vector<vector<int>> adj, c;
int bfs(int s, int t, vector<int> &parent) {
fill(parent.begin(), parent.end(), -1);
parent[s] = -2; // visited, with no parent
queue<pair<int, int>> q;
q.push({s, INF});
while (!q.empty()) {
int u = q.front().first;
int flow = q.front().second;
q.pop();
for (int v : adj[u]) {
if (parent[v] == -1 && c[u][v]) {
parent[v] = u;
int new_flow = min(flow, c[u][v]);
if (v == t)
return new_flow;
q.push({v, new_flow});
}
}
}
return 0;
}
int edmondskarp(int s, int t, int n) {
int flux = 0, new_flux;
vector<int> parent(n + 1);
while (new_flux = bfs(s, t, parent)) {
flux += new_flux;
int cur = t;
while (cur != s) {
int prev = parent[cur];
c[prev][cur] -= new_flux;
c[cur][prev] += new_flux;
cur = prev;
}
}
return flux;
}
int main() {
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int n, m;
cin >> n >> m;
adj.assign(n + 1, vector<int>());
c.assign(n + 1, vector<int>(n + 1, 0));
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back(v);
adj[v].push_back(u);
c[u][v] = w;
}
cout << edmondskarp(1, n, n);
return 0;
}