#include <bits/stdc++.h>
#define dbg(x) cerr<<#x": "<<x<<"\n"
#define dbg_p(x) cerr<<#x": "<<x.first<<","<<x.second<<"\n"
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define DMAX 10100
#define NMAX 1
#define MMAX 1
using namespace std;
int n, k, x, m, src, dest, use[DMAX], from, to, cost, flow;
string s;
struct edge{
int x, y, c, f;
edge() {}
edge(int _x, int _y, int _c, int _f)
{
x = _x;
y = _y;
c = _c;
f = _f;
}
};
vector <int> v[DMAX];
edge e[DMAX];
int lge;
int dfs(int node, int flow, int limit)
{
if(!flow || node == dest)
return flow;
for(auto i : v[node])
{
if(use[e[i].y] || (e[i].c < limit && e[i^1].c < limit))
continue;
if(e[i].f < e[i].c)
{
int u = e[i].y;
use[u] = 1;
int recv = dfs(u, min(flow, e[i].c - e[i].f), limit);
use[u] = 0;
e[i].f += recv;
e[i ^ 1].f -= recv;
if(recv > 0)
return recv;
}
}
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
//ios_base::sync_with_stdio(false);
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> from >> to >> cost;
v[from].push_back(lge);
e[lge++] = edge(from, to, cost, 0);
v[to].push_back(lge);
e[lge++] = edge(to, from, 0, 0);
}
//cin >> src >> dest;
src = 1;
dest = n;
use[src] = 1;
for(int i = 16; i >=0; i--)
{
int fl = dfs(src, 120000, (1 << i));
while(fl > 0) {
memset(use, 0, sizeof use);
flow += fl;
use[1] = 1;
fl = dfs(src, 120000, (1 << i));
}
}
cout << flow << '\n';
}