#include <bits/stdc++.h>
#define inf 1000000007
using namespace std;
struct Dinic {
struct Edge {
int to, cap, next;
};
vector <Edge> edges;
vector <int> head, act, h;
int S, D, n;
void add_edge(int from, int to, int cap) {
Edge a = { to, cap, edges.size() };
swap(head[from], a.next);
edges.push_back(a);
a = { from, 0, edges.size() };
swap(head[to], a.next);
edges.push_back(a);
}
bool bfs() {
fill(h.begin(), h.begin() + n, -1);
h[S] = 1;
vector <int> q = { S };
for (int top = 0; top < q.size(); top++) {
int nod = q[top];
for (int i = head[nod]; ~i; i = edges[i].next)
if (edges[i].cap && h[edges[i].to] == -1)
h[edges[i].to] = 1 + h[nod], q.push_back(edges[i].to);
}
return ~h[D];
}
int dfs(int nod, int cap_max) {
/// returns how much it used
if (!cap_max || nod == D)
return cap_max;
int used = 0, d;
while (~act[nod] && cap_max) {
Edge & E = edges[act[nod]];
if (h[E.to] == 1 + h[nod] && E.cap && (d = dfs(E.to, min(cap_max, E.cap)))) {
used += d;
E.cap -= d;
edges[act[nod] ^ 1].cap += d;
cap_max -= d;
}
else
act[nod] = E.next;
}
return used;
}
int get_flow() {
int f = 0;
while (bfs()) {
act = head;
f += dfs(S, inf);
}
return f;
}
Dinic(int n = 0, int S = 0, int D = 0) : n(n + 1), S(S), D(D), h(vector <int>(n + 1)),
head(vector <int> (n + 1, -1)) { }
};
int main()
{
FILE *in = fopen("maxflow.in", "r");
FILE *out = fopen("maxflow.out", "w");
int n, m, a, b, c;
fscanf(in, "%d%d", &n, &m);
Dinic Flow(n, 1, n);
while (m--) {
fscanf(in, "%d%d%d", &a, &b, &c);
Flow.add_edge(a, b, c);
}
fprintf(out, "%d\n", Flow.get_flow());
return 0;
}