#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define FOR(i, a, b) for(ll i = (a); i < (b); i++)
#define FORD(i, a, b) for(ll i = (b)-1; i >= (a); i--)
typedef vector<int> VI;
typedef vector<VI> VVI;
const ll INF = 1000000000000000000LL;
#define VEI(w,e) ((E[e].u == w) ? E[e].v : E[e].u)
#define CAP(w,e) ((E[e].u == w) ? E[e].cap[0] - E[e].flow : E[e].cap[1] + E[e].flow)
#define ADD(w,e,f) E[e].flow += ((E[e].u == w) ? (f) : (-(f)))
struct Edge { int u, v; ll cap[2], flow; };
VI d, act;
bool bfs(int s, int t, VVI& adj, vector<Edge>& E) {
queue<int> Q;
d = VI(adj.size(), -1);
d[t] = 0;
Q.push(t);
while (not Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = 0; i < int(adj[u].size()); ++i) {
int e = adj[u][i], v = VEI(u, e);
if (CAP(v, e) > 0 and d[v] == -1) {
d[v] = d[u] + 1;
Q.push(v);
}
}
}
return d[s] >= 0;
}
ll dfs(int u,int t,ll bot,VVI& adj,vector<Edge>& E) {
if (u == t) return bot;
for (; act[u] < int(adj[u].size()); ++act[u]) {
int e = adj[u][act[u]];
if (CAP(u, e) > 0 and d[u] == d[VEI(u, e)] + 1) {
ll inc=dfs(VEI(u,e),t,min(bot,CAP(u,e)),adj,E);
if (inc) {
ADD(u, e, inc);
return inc;
}
}
}
return 0;
}
ll maxflow(int s, int t, VVI& adj, vector<Edge>& E) {
for (int i=0; i<int(E.size()); ++i) E[i].flow = 0;
ll flow = 0, bot;
while (bfs(s, t, adj, E)) {
act = VI(adj.size(), 0);
while ((bot = dfs(s,t,INF, adj, E))) flow += bot;
}
return flow;
}
void addEdge(int u, int v, VVI& adj, vector<Edge>& E, ll cap){
Edge e;
e.u = u;
e.v = v;
e.cap[0] = cap;
e.cap[1] = cap;
e.flow = 0;
adj[u].push_back(E.size());
adj[v].push_back(E.size());
E.push_back(e);
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>>adj(n);
vector<Edge> E;
for(int i = 0; i < m; ++i) {
int x,y,w;
cin >> x >> y >> w;
--x;--y;
addEdge(x, y, adj, E, w);
}
cout << maxflow(0, n-1, adj ,E) << endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIGxsID0gbG9uZyBsb25nOwp1c2luZyB2bCA9IHZlY3RvcjxsbD47CiNkZWZpbmUgRk9SKGksIGEsIGIpIGZvcihsbCBpID0gKGEpOyBpIDwgKGIpOyBpKyspCiNkZWZpbmUgRk9SRChpLCBhLCBiKSBmb3IobGwgaSA9IChiKS0xOyBpID49IChhKTsgaS0tKQogCnR5cGVkZWYgdmVjdG9yPGludD4gVkk7CnR5cGVkZWYgdmVjdG9yPFZJPiBWVkk7CmNvbnN0IGxsIElORiA9IDEwMDAwMDAwMDAwMDAwMDAwMDBMTDsKIAojZGVmaW5lIFZFSSh3LGUpICgoRVtlXS51ID09IHcpID8gRVtlXS52IDogRVtlXS51KQojZGVmaW5lIENBUCh3LGUpICgoRVtlXS51ID09IHcpID8gRVtlXS5jYXBbMF0gLSBFW2VdLmZsb3cgOiBFW2VdLmNhcFsxXSArIEVbZV0uZmxvdykKI2RlZmluZSBBREQodyxlLGYpIEVbZV0uZmxvdyArPSAoKEVbZV0udSA9PSB3KSA/IChmKSA6ICgtKGYpKSkKIApzdHJ1Y3QgRWRnZSB7IGludCB1LCB2OyBsbCBjYXBbMl0sIGZsb3c7IH07CiAKVkkgZCwgYWN0OwogCmJvb2wgYmZzKGludCBzLCBpbnQgdCwgVlZJJiBhZGosIHZlY3RvcjxFZGdlPiYgRSkgewogIHF1ZXVlPGludD4gUTsKICBkID0gVkkoYWRqLnNpemUoKSwgLTEpOwogIGRbdF0gPSAwOwogIFEucHVzaCh0KTsKICB3aGlsZSAobm90IFEuZW1wdHkoKSkgewogICAgaW50IHUgPSBRLmZyb250KCk7IFEucG9wKCk7CiAgICBmb3Ig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