#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 5005;
template <int MAXN, class T = int> struct HLPP {
const T INF = numeric_limits<T>::max();
struct edge {
int to, rev;
T f;
};
int s = MAXN - 1, t = MAXN - 2;
vector<edge> adj[MAXN];
vector<int> lst[MAXN], gap[MAXN];
T excess[MAXN];
int highest, height[MAXN], cnt[MAXN], work;
void addEdge(int from, int to, T f, bool isDirected = true) {
adj[from].push_back({to, adj[to].size(), f});
adj[to].push_back({from, adj[from].size() - 1, isDirected ? 0 : f});
}
void updHeight(int v, int nh) {
work++;
if (height[v] != MAXN)
cnt[height[v]]--;
height[v] = nh;
if (nh == MAXN)
return;
cnt[nh]++, highest = nh;
gap[nh].push_back(v);
if (excess[v] > 0)
lst[nh].push_back(v);
}
void globalRelabel() {
work = 0;
fill(height, height + MAXN, MAXN);
fill(cnt, cnt + MAXN, 0);
for (int i = 0; i < highest; i++)
lst[i].clear(), gap[i].clear();
height[t] = 0;
queue<int> q({t});
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto &e : adj[v])
if (height[e.to] == MAXN && adj[e.to][e.rev].f > 0)
q.push(e.to), updHeight(e.to, height[v] + 1);
highest = height[v];
}
}
void push(int v, edge &e) {
if (excess[e.to] == 0)
lst[height[e.to]].push_back(e.to);
T df = min(excess[v], e.f);
e.f -= df, adj[e.to][e.rev].f += df;
excess[v] -= df, excess[e.to] += df;
}
void discharge(int v) {
int nh = MAXN;
for (auto &e : adj[v]) {
if (e.f > 0) {
if (height[v] == height[e.to] + 1) {
push(v, e);
if (excess[v] <= 0)
return;
} else
nh = min(nh, height[e.to] + 1);
}
}
if (cnt[height[v]] > 1)
updHeight(v, nh);
else {
for (int i = height[v]; i <= highest; i++) {
for (auto j : gap[i])
updHeight(j, MAXN);
gap[i].clear();
}
}
}
T calc(int heur_n = MAXN) {
fill(begin(excess), end(excess), 0);
excess[s] = INF, excess[t] = -INF;
globalRelabel();
for (auto &e : adj[s])
push(s, e);
for (; highest >= 0; highest--) {
while (!lst[highest].empty()) {
int v = lst[highest].back();
lst[highest].pop_back();
discharge(v);
if (work > 4 * heur_n)
globalRelabel();
}
}
return excess[t] + INF;
}
};
void read_uint() {}
template <class T, class... S> inline void read_uint(T &a, S &... b) {
static char c;
while (isspace(c = getchar_unlocked()))
;
for (a = c - '0'; isdigit(c = getchar_unlocked()); a = a * 10 + c - '0')
;
read_uint(b...);
}
HLPP<MAXN, ll> hlpp;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, p;
read_uint(n, m);
int s = 1, t = n;
// read_uint(s), read_uint(t);
for (int i = 0, u, v, f; i < m; ++i) {
read_uint(u), read_uint(v), read_uint(f);
hlpp.addEdge(u, v, f, false);
}
hlpp.s = s, hlpp.t = t;
ll ans = hlpp.calc(n);
cout << ans << endl;
return 0;
// vector<array<int, 3>> res;
// for (int i = 0; i < MAXN; i++) {
// for (auto j : hlpp.adj[i]) {
// if (j.f < j.orig)
// res.push_back({i, j.to, j.orig - j.f});
// }
// // if (hlpp.adj[i].size())
// // cout << endl;
// }
// cout << n << ' ' << ans << ' ' << res.size() << endl;
// for (auto i : res) {
// cout << i[0] << ' ' << i[1] << ' ' << i[2] << endl;
// }
}
I3ByYWdtYSBHQ0Mgb3B0aW1pemUoMykKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKY29uc3QgaW50IE1BWE4gPSA1MDA1OwogCnRlbXBsYXRlIDxpbnQgTUFYTiwgY2xhc3MgVCA9IGludD4gc3RydWN0IEhMUFAgewogICAgY29uc3QgVCBJTkYgPSBudW1lcmljX2xpbWl0czxUPjo6bWF4KCk7CiAgICBzdHJ1Y3QgZWRnZSB7CiAgICAgICAgaW50IHRvLCByZXY7CiAgICAgICAgVCBmOwogICAgfTsKICAgIGludCBzID0gTUFYTiAtIDEsIHQgPSBNQVhOIC0gMjsKICAg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LmNsZWFyKCk7CiAgICAgICAgaGVpZ2h0W3RdID0gMDsKICAgICAgICBxdWV1ZTxpbnQ+IHEoe3R9KTsKICAgICAgICB3aGlsZSAoIXEuZW1wdHkoKSkgewogICAgICAgICAgICBpbnQgdiA9IHEuZnJvbnQoKTsKICAgICAgICAgICAgcS5wb3AoKTsKICAgICAgICAgICAgZm9yIChhdXRvICZlIDogYWRqW3ZdKQogICAgICAgICAgICAgICAgaWYgKGhlaWdodFtlLnRvXSA9PSBNQVhOICYmIGFkaltlLnRvXVtlLnJldl0uZiA+IDApCiAgICAgICAgICAgICAgICAgICAgcS5wdXNoKGUudG8pLCB1cGRIZWlnaHQoZS50bywgaGVpZ2h0W3ZdICsgMSk7CiAgICAgICAgICAgIGhpZ2hlc3QgPSBoZWlnaHRbdl07CiAgICAgICAgfQogICAgfQogICAgdm9pZCBwdXNoKGludCB2LCBlZGdlICZlKSB7CiAgICAgICAgaWYgKGV4Y2Vzc1tlLnRvXSA9PSAwKQogICAgICAgICAgICBsc3RbaGVpZ2h0W2UudG9dXS5wdXNoX2JhY2soZS50byk7CiAgICAgICAgVCBkZiA9IG1pbihleGNlc3Nbdl0sIGUuZik7CiAgICAgICAgZS5mIC09IGRmLCBhZGpbZS50b11bZS5yZXZdLmYgKz0gZGY7CiAgICAgICAgZXhjZXNzW3ZdIC09IGRmLCBleGNlc3NbZS50b10gKz0gZGY7CiAgICB9CiAgICB2b2lkIGRpc2NoYXJnZShpbnQgdikgewogICAgICAgIGludCBuaCA9IE1BWE47CiAgICAgICAgZm9yIChhdXRvICZlIDogYWRqW3ZdKSB7CiAgICAgICAgICAgIGlmIChlLmYgPiAwKSB7CiAgICAgICAgICAgICAgICBpZiAoaGVpZ2h0W3ZdID09IGhlaWdodFtlLnRvXSArIDEpIHsKICAgICAgICAgICAgICAgICAgICBwdXNoKHYsIGUpOwogICAgICAgICAgICAgICAgICAgIGlmIChleGNlc3Nbdl0gPD0gMCkKICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICAgICAgfSBlbHNlCiAgICAgICAgICAgICAgICAgICAgbmggPSBtaW4obmgsIGhlaWdodFtlLnRvXSArIDEpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChjbnRbaGVpZ2h0W3ZdXSA+IDEpCiAgICAgICAgICAgIHVwZEhlaWdodCh2LCBuaCk7CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSBoZWlnaHRbdl07IGkgPD0gaGlnaGVzdDsgaSsrKSB7CiAgICAgICAgICAgICAgICBmb3IgKGF1dG8gaiA6IGdhcFtpXSkKICAgICAgICAgICAgICAgICAgICB1cGRIZWlnaHQoaiwgTUFYTik7CiAgICAgICAgICAgICAgICBnYXBbaV0uY2xlYXIoKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIFQgY2FsYyhpbnQgaGV1cl9uID0gTUFYTikgewogICAgICAgIGZpbGwoYmVnaW4oZXhjZXNzKSwgZW5kKGV4Y2VzcyksIDApOwogICAgICAgIGV4Y2Vzc1tzXSA9IElORiwgZXhjZXNzW3RdID0gLUlORjsKICAgICAgICBnbG9iYWxSZWxhYmVsKCk7CiAgICAgICAgZm9yIChhdXRvICZlIDogYWRqW3NdKQogICAgICAgICAgICBwdXNoKHMsIGUpOwogICAgICAgIGZvciAoOyBoaWdoZXN0ID49IDA7IGhpZ2hlc3QtLSkgewogICAgICAgICAgICB3aGlsZSAoIWxzdFtoaWdoZXN0XS5lbXB0eSgpKSB7CiAgICAgICAgICAgICAgICBpbnQgdiA9IGxzdFtoaWdoZXN0XS5iYWNrKCk7CiAgICAgICAgICAgICAgICBsc3RbaGlnaGVzdF0ucG9wX2JhY2soKTsKICAgICAgICAgICAgICAgIGRpc2NoYXJnZSh2KTsKICAgICAgICAgICAgICAgIGlmICh3b3JrID4gNCAqIGhldXJfbikKICAgICAgICAgICAgICAgICAgICBnbG9iYWxSZWxhYmVsKCk7CiAgICAgICAgIC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