#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 5001;
const int INF = 1000000000;
vector<int> g[MAXN] ;
int n, m, c[MAXN][MAXN], f[MAXN][MAXN], s, t, d[MAXN], ptr[MAXN], q[MAXN];
bool bfs() {
queue<int> q ;
memset (d, -1, sizeof d);
q.push(s) ;
d[s] = 0 ;
while(!q.empty())
{
int v = q.front() ;
q.pop() ;
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i] ;
if(d[to] == -1 and f[v][to] < c[v][to])
{
q.push(to) ;
d[to] = d[v] + 1 ;
}
}
}
return d[t] != -1 ;
}
int dfs (int v, int flow) {
if (!flow) return 0;
if (v == t) return flow;
for (int & to=ptr[v]; to <= n; ++to) {
if (d[to] != d[v] + 1) continue;
int pushed = dfs (to, min (flow, c[v][to] - f[v][to]));
if (pushed) {
f[v][to] += pushed;
f[to][v] -= pushed;
return pushed;
}
}
return 0;
}
ll dinic() {
ll flow = 0;
for (;;) {
if (!bfs()) break;
memset (ptr, 0, n * sizeof ptr[0]);
while (int pushed = dfs (s, INF))
flow += (1LL * pushed);
}
return flow;
}
int main() {
//ifstream cin("input.txt") ;
//ofstream cout("output.txt") ;
cin.sync_with_stdio(false) ;
cin.tie(NULL) ;
cin >> n >> m ;
while(m--)
{
int n1, n2, cap;
cin >> n1 >> n2 >> cap ;
if(n1 == n2)
continue ;
g[n1].push_back(n2) ;
g[n2].push_back(n1) ;
c[n1][n2] += cap ;
c[n2][n1] += cap ;
}
s = 1, t = n ;
cout << dinic() ;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTUFYTiA9IDUwMDE7CmNvbnN0IGludCBJTkYgPSAxMDAwMDAwMDAwOwp2ZWN0b3I8aW50PiBnW01BWE5dIDsKCmludCBuLCBtLCBjW01BWE5dW01BWE5dLCBmW01BWE5dW01BWE5dLCBzLCB0LCBkW01BWE5dLCBwdHJbTUFYTl0sIHFbTUFYTl07Cgpib29sIGJmcygpIHsKICAgICAgICBxdWV1ZTxpbnQ+IHEgOwoJbWVtc2V0IChkLCAtMSwgc2l6ZW9mIGQpOwoJcS5wdXNoKHMpIDsKCWRbc10gPSAwIDsKCXdoaWxlKCFxLmVtcHR5KCkpCiAgICAgICAgewogICAgICAgICAgICAgICAgaW50IHYgPSBxLmZyb250KCkgOwogICAgICAgICAgICAgICAgcS5wb3AoKSA7CiAgICAgICAgICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgZ1t2XS5zaXplKCk7IGkrKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgaW50IHRvID0gZ1t2XVtpXSA7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKGRbdG9dID09IC0xIGFuZCBmW3ZdW3RvXSA8IGNbdl1bdG9dKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcS5wdXNoKHRvKSA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZFt0b10gPSBkW3ZdICsgMSA7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICB9CglyZXR1cm4gZFt0XSAhPSAtMSA7Cn0KCmludCBkZnMgKGludCB2LCBpbnQgZmxvdykgewoJaWYgKCFmbG93KSAgcmV0dXJuIDA7CglpZiAodiA9PSB0KSAgcmV0dXJuIGZsb3c7Cglmb3IgKGludCAmIHRvPXB0clt2XTsgdG8gPD0gbjsgKyt0bykgewoJCWlmIChkW3RvXSAhPSBkW3ZdICsgMSkgIGNvbnRpbnVlOwoJCWludCBwdXNoZWQgPSBkZnMgKHRvLCBtaW4gKGZsb3csIGNbdl1bdG9dIC0gZlt2XVt0b10pKTsKCQlpZiAocHVzaGVkKSB7CgkJCWZbdl1bdG9dICs9IHB1c2hlZDsKCQkJZlt0b11bdl0gLT0gc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