#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const string txt = "maxflow";
const int inf = 1e9;
ifstream f(txt + ".in");
ofstream g(txt + ".out");
struct ceva {
int vec, cost;
};
int n, m, t[5005], viz[5005], flow[1005][1005];
vector<int> v[5005];
bool bfs()
{
queue<int> q;
for (int i = 1; i <= n; i++) t[i] = viz[i] = 0;
while (!q.empty()) q.pop();
q.push(1); viz[1] = 1;
while (!q.empty())
{
int nod = q.front(); q.pop();
for (auto x : v[nod])
{
if (viz[x] || flow[nod][x] <= 0) continue;
t[x] = nod; viz[x] = 1; q.push(x);
}
}
return viz[n];
}
int main()
{
f >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, c; f >> x >> y >> c;
flow[x][y] = c;
v[x].push_back(y);
v[y].push_back(x);
}
long long max_flow = 0;
while (bfs()) {
int ans = inf;
for (int node = n; node != 1; node = t[node])
ans = min(ans, flow[t[node]][node]);
for (int node = n; node != 1; node = t[node]) {
flow[t[node]][node] -= ans;
flow[node][t[node]] += ans;
}
max_flow += ans;
}
g << max_flow;
return 0;
}